Py学习  »  Python

独家 | 手把手教你用Python进行Web抓取(附代码)

数据派THU • 5 年前 • 1056 次点击  

作者:Kerry Parker 

翻译:田晓宁

校对:丁楠雅

本文2900字,建议阅读10分钟

本教程以在Fast Track上收集百强公司的数据为例,教你抓取网页信息。



作为一名数据科学家,我在工作中所做的第一件事就是网络数据采集。使用代码从网站收集数据,当时对我来说是一个完全陌生的概念,但它是最合理、最容易获取的数据来源之一。经过几次尝试,网络抓取已经成为我的第二天性,也是我几乎每天使用的技能之一。


在本教程中,我将介绍一个简单的例子,说明如何抓取一个网站,我将从Fast Track上收集2018年百强公司的数据:


Fast Track:

http://www.fasttrack.co.uk/


使用网络爬虫将此过程自动化,避免了手工收集数据,节省了时间,还可以让所有数据都放在一个结构化文件中。


用Python实现一个简单的网络爬虫的快速示例,您可以在GitHub上找到本教程中所介绍的完整代码。


GitHub链接:

https://github.com/kaparker/tutorials/blob/master/pythonscraper/websitescrapefasttrack.py


以下是本文使用Python进行网页抓取的简短教程概述:


  • 连接到网页

  • 使用BeautifulSoup解析html

  • 循环通过soup对象找到元素

  • 执行一些简单的数据清理

  • 将数据写入csv


准备开始


在开始使用任何Python应用程序之前,要问的第一个问题是:我需要哪些库?


对于web抓取,有一些不同的库需要考虑,包括:


  • Beautiful Soup

  • Requests

  • Scrapy

  • Selenium


在本例中我们使用Beautiful Soup。你可以使用Python包管理器 pip 安装Beautiful Soup:


pip install BeautifulSoup4


安装好这些库之后,让我们开始吧!


检查网页


要知道在Python代码中需要定位哪些元素,首先需要检查网页。


要从Tech Track Top 100 companies收集数据,可以通过右键单击感兴趣的元素来检查页面,然后选择检查。这将打开HTML代码,我们可以在其中看到每个字段包含在其中的元素。


Tech Track Top 100 companies链接:

http://www.fasttrack.co.uk/league-tables/tech-track-100/league-table/

 


右键单击感兴趣的元素并选择“Inspect”,显示html元素。


由于数据存储在一个表中,因此只需几行代码就可以直接获取数据。如果您想练习抓取网站,这是一个很好的例子,也是一个好的开始,但请记住,它并不总是那么简单!


所有100个结果都包含在 元素的行中,并且这些在一页上都可见。情况并非总是如此,当结果跨越多个页面时,您可能需要更改网页上显示的结果数量,或者遍历所有页面以收集所有信息。


League Table网页上显示了包含100个结果的表。检查页面时,很容易在html中看到一个模式。结果包含在表格中的行中:


class="tableSorter">


重复的行

 将通过在Python中使用循环来查找数据并写入文件来保持我们的代码最小化!


附注:可以做的另一项检查是网站上是否发出了HTTP GET请求,该请求可能已经将结果作为结构化响应(如JSON或XML格式)返回。您可以在检查工具的网络选项卡中进行检查,通常在XHR选项卡中进行检查。刷新页面后,它将在加载时显示请求,如果响应包含格式化结构,则使用REST客户端(如Insomnia)返回输出通常更容易。


刷新网页后,页面检查工具的网络选项卡


使用Beautiful Soup解析网页html


现在您已经查看了html的结构并熟悉了将要抓取的内容,是时候开始使用Python了!


第一步是导入将用于网络爬虫的库。我们已经讨论过上面的BeautifulSoup,它有助于我们处理html。我们导入的下一个库是urllib,它连接到网页。最后,我们将输出写入csv,因此我们还需要导入csv 库。作为替代方案,可以在此处使用json库。


# import libraries
from bs4 import BeautifulSoup
import urllib.request
import csv


下一步是定义您正在抓取的网址。如上一节所述,此网页在一个页面上显示所有结果,因此此处给出了地址栏中的完整url:


# specify the url
urlpage =  'http://www.fasttrack.co.uk/league-tables/tech-track-100/league-table/'


然后我们建立与网页的连接,我们可以使用BeautifulSoup解析html,将对象存储在变量'soup'中:


# query the website and return the html to the variable 'page'
page = urllib.request.urlopen(urlpage)
# parse the html using beautiful soup and store in variable 'soup'
soup = BeautifulSoup(page, 'html.parser')


我们可以在这个阶段打印soup变量,它应该返回我们请求网页的完整解析的html。


print(soup)


如果存在错误或变量为空,则请求可能不成功。可以使用urllib.error模块在此时实现错误处理。


搜索html元素


由于所有结果都包含在表中,我们可以使用find 方法搜索表的soup对象。然后我们可以使用find_all 方法查找表中的每一行。


如果我们打印行数,我们应该得到101的结果,100行加上标题。


# find results within table
table = soup.find('table', attrs={'class': 'tableSorter'})
results = table.find_all('tr')
print('Number of results', len(results))


因此,我们可以对结果进行循环以收集数据。


打印soup对象的前两行,我们可以看到每行的结构是:
























表格中有8栏:Rank,Company,Location,Year End,Annual Sales Rise,Latest Sales, Staff and Comments,所有这些都是我们可以保存的感兴趣的数据。


网页的所有行的结构都是一致的(对于所有网站来说可能并非总是如此!)。因此,我们可以再次使用find_all 方法将每一列分配给一个变量,那么我们可以通过搜索


要将company 分成两个字段,我们可以使用find方法保存元素,然后使用strip 或replace 从company 变量中删除公司名称,这样它只留下描述。


要从sales中删除不需要的字符,我们可以再次使用strip和replace 方法!


    # extract description from the name
    companyname = data[1].find('span', attrs={'class':'company-name'}).getText()    
    description = company.replace(companyname, '')
    
    # remove unwanted characters
    sales = sales.strip('*').strip('†').replace(',','')


我们要保存的最后一个变量是公司网站。如上所述,第二列包含指向另一个页面的链接,该页面具有每个公司的概述。 每个公司页面都有自己的表格,大部分时间都包含公司网站。

 

检查公司页面上的url元素


要从每个表中抓取url并将其保存为变量,我们需要使用与上面相同的步骤:


  • 在fast track网站上找到具有公司页面网址的元素

  • 向每个公司页面网址发出请求

  • 使用Beautifulsoup解析html

  • 找到感兴趣的元素


查看一些公司页面,如上面的屏幕截图所示,网址位于表格的最后一行,因此我们可以在最后一行内搜索元素。


    # go to link and extract company website
    url = data[1].find('a').get('href')
    page = urllib.request.urlopen(url)
    # parse the html 
    soup = BeautifulSoup(page, 'html.parser')
    # find the last result in the table and get the link
    try:
        tableRow = soup.find('table').find_all('tr')[-1]
        webpage = tableRow.find('a').get('href')
    except:
        webpage = None


也有可能出现公司网站未显示的情况,因此我们可以使用try except条件,以防万一找不到网址。


一旦我们将所有数据保存到变量中,我们可以在循环中将每个结果添加到列表rows。


    # write each result to rows
    rows.append([rank, company, webpage, description, location, yearend, salesrise, sales, staff, comments])

print(rows)


然后可以试着在循环外打印变量,在将其写入文件之前检查它是否符合您的预期!


写入输出文件


如果想保存此数据以进行分析,可以用Python从我们列表中非常简单地实现。


# Create csv and write rows to output file
with open('techtrack100.csv','w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerows(rows)


运行Python脚本时,将生成包含100行结果的输出文件,您可以更详细地查看这些结果!


尾语


这是我的第一个教程,如果您有任何问题或意见或者不清楚的地方,请告诉我!


  • Web Development

    https://towardsdatascience.com/tagged/web-development?source=post

  • Python

    https://towardsdatascience.com/tagged/python?source=post

  • Web Scraping

    https://towardsdatascience.com/tagged/web-scraping?source=post

  • Data Science

    https://towardsdatascience.com/tagged/data-science?source=post

  • Programming

    https://towardsdatascience.com/tagged/programming?source=post

 

原文标题:

Data Science Skills: Web scraping using python

原文链接: 

https://towardsdatascience.com/data-science-skills-web-scraping-using-python-d1a85ef607ed


译者简介


田晓宁,质量管理专家,国际认证精益六西格玛黑带,19年从业经验;软件工程专家,拥有CMMI ATM证书,曾主导公司通过CMMI 5级评估;精通ISO9000和ISO27000体系,长期担任公司质量和信息安全主任审核员,每年审核超过50个项目或部门;拥有PMP证书,担任公司项目管理内训师,具有项目管理和系统开发实战经验。

翻译组招募信息

工作内容:需要一颗细致的心,将选取好的外文文章翻译成流畅的中文。如果你是数据科学/统计学/计算机类的留学生,或在海外从事相关工作,或对自己外语水平有信心的朋友欢迎加入翻译小组。

你能得到:定期的翻译培训提高志愿者的翻译水平,提高对于数据科学前沿的认知,海外的朋友可以和国内技术应用发展保持联系,THU数据派产学研的背景为志愿者带来好的发展机遇。

其他福利:来自于名企的数据科学工作者,北大清华以及海外等名校学生他们都将成为你在翻译小组的伙伴。


点击文末“阅读原文”加入数据派团队~

转载须知

如需转载,请在开篇显著位置注明作者和出处(转自:数据派ID:datapi),并在文章结尾放置数据派醒目二维码。有原创标识文章,请发送【文章名称-待授权公众号名称及ID】至联系邮箱,申请白名单授权并按要求编辑。

发布后请将链接反馈至联系邮箱(见下方)。未经许可的转载以及改编者,我们将依法追究其法律责任。


点击“阅读原文”拥抱组织

    阅读原文
    max_width ? max_width : initWidth; if (initWidth) { imageItem.setAttribute('_width', !isNaN(initWidth * 1) ? initWidth + 'px' : initWidth); } if (typeof initWidth === 'string' && initWidth.indexOf('%') !== -1) { initWidth = parseFloat(initWidth.replace('%', ''), 10) / 100 * parent_width; } if (initWidth === 'auto') { initWidth = originWidth; } var res = /^(\d+(?:\.\d+)?)([a-zA-Z%]+)?$/.exec(initWidth); var widthNum = res && res.length >= 2 ? res[1] : 0; var widthUnit = res && res.length >= 3 && res[2] ? res[2] : 'px'; setImgSize(imageItem, widthNum, widthUnit, ratio_, true); (function (item, widthNumber, unit, ratio) { setTimeout(function () { setImgSize(item, widthNumber, unit, ratio, false); }); })(imageItem, widthNum, widthUnit, ratio_); } else { imageItem.style.cssText += ";visibility: hidden !important;"; } } })(); window.__videoDefaultRatio=16/9; window.__getVideoWh = function(dom){ var max_width = getMaxWith(), width = max_width, ratio_ = dom.getAttribute('data-ratio')*1, arr = [4/3, 16/9], ret = arr[0], abs = Math.abs(ret - ratio_); if (!ratio_) { if (dom.getAttribute("data-mpvid")) { ratio_ = 16/9; } else { ratio_ = 4/3; } } else { for (var j = 1, jl = arr.length; j < jl; j++) { var _abs = Math.abs(arr[j] - ratio_); if (_abs < abs) { abs = _abs; ret = arr[j]; } } ratio_ = ret; } var parent_width = getParentWidth(dom)||max_width, width = width > parent_width ? parent_width : width, outerW = getOuterW(dom)||0, outerH = getOuterH(dom)||0, videoW = width - outerW, videoH = videoW/ratio_, height = videoH + outerH; return {w:Math.ceil(width),h:Math.ceil(height),vh:videoH,vw:videoW,ratio:ratio_}; }; (function(){ var iframe = document.getElementsByTagName('iframe'); for (var i=0,il=iframe.length;i=0 && child.getAttribute("data-vid")==vid){ child.style.cssText += "height: " + h + "px !important;"; child.style.display = ""; } } } } })(); })(); var new_appmsg = 1; var item_show_type = "0"; var can_see_complaint = "0"; var not_in_mm_css = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/page/appmsg_new/not_in_mm3ec991.css"; var windowwx_css = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/page/appmsg_new/winwx3ec991.css"; var article_improve_combo_css = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/page/appmsg_new/combo41834c.css"; var tid = ""; var aid = ""; var clientversion = ""; var appuin = "MzI1MjQ2OTQ3Ng=="||""; var source = "0"; var ascene = ""; var subscene = ""; var sessionid = ""; var abtest_cookie = ""; var scene = 75; var itemidx = ""; var appmsg_token = ""; var _copyright_stat = "1"; var _ori_article_type = "科技互联网"; var is_follow = ""; var nickname = "数据派THU"; var appmsg_type = "9"; var ct = "1542798000"; var publish_time = "2018-11-21" || ""; var user_name = "gh_0f832a4b2e4d"; var user_name_new = ""; var fakeid = ""; var version = ""; var is_limit_user = "0"; var round_head_img = "http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMlfFF4kOeEicO2q15ibSnhWxgOZ7uIRWLcbawQwnibDSd0OURUTCicR2YpibbtxGsr3M7aql3a9j2vNiaOA/0?wx_fmt=png"; var hd_head_img = "http://wx.qlogo.cn/mmhead/Q3auHgzwzM48ayvz1uibnIzEqzb3z6EgfcN6sJW6L2sTIWUibcpicqWog/0"||""; var ori_head_img_url = "http://wx.qlogo.cn/mmhead/Q3auHgzwzM48ayvz1uibnIzEqzb3z6EgfcN6sJW6L2sTIWUibcpicqWog/132"; var msg_title = "独家 | 手把手教你用Python进行Web抓取(附代码)"; var msg_desc = "本教程以在Fast Track上收集百强公司的数据为例,教你抓取网页信息。"; var msg_cdn_url = "http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/heS6wRSHVMnPeNOh1SfmsYm94FKicyTQkSLAh2JFicXIKic4Y2y7OMyBSgA5ZhGicGcPJcibwBKDaVZvg3TEmfzeVEA/0?wx_fmt=jpeg"; var cdn_url_1_1 = "http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/heS6wRSHVMnPeNOh1SfmsYm94FKicyTQkIgDIW7ibQFftUaFhK1fVicko0TaXDlmKOIU2gHSQCSQ15wia8JTDx6ysw/0?wx_fmt=jpeg"; var cdn_url_235_1 = "http://img2.jintiankansha.me/get?src=http://mmbiz.qpic.cn/mmbiz_jpg/heS6wRSHVMnPeNOh1SfmsYm94FKicyTQkSLAh2JFicXIKic4Y2y7OMyBSgA5ZhGicGcPJcibwBKDaVZvg3TEmfzeVEA/0?wx_fmt=jpeg"; var msg_link = "http://mp.weixin.qq.com/s?__biz=MzI1MjQ2OTQ3Ng==&mid=2247492739&idx=1&sn=51e60287f6413c114e0fa4c0d6354224&chksm=e9e1ed08de96641ef2bf8cce3b4614abd534a1a0fd668d627a50d9a75a9750fc36d512903260#rd"; var user_uin = "0"*1; var msg_source_url = 'https://mp.weixin.qq.com/s/6MnEJ7Vah_9PVYyIVftdzQ?scene=25#wechat_redirect'; var img_format = 'jpeg'; var srcid = ''; var req_id = '21206x22Jo9pxxDbze0mhSf6'; var networkType; var appmsgid = '' || '2247492739'|| ""; var comment_id = "555559630391066630" || "555559630391066630" * 1; var comment_enabled = "" * 1; var is_need_reward = "0" * 1; var is_https_res = ("" * 1) && (location.protocol == "https:"); var msg_daily_idx = "1" || ""; var profileReportInfo = "" || ""; var devicetype = ""; var source_encode_biz = ""; var source_username = ""; var reprint_ticket = ""; var source_mid = ""; var source_idx = ""; var source_biz = ""; var author_id = ""; var optimizing_flag = "0" * 1; var ad_abtest_padding = "0" * 1; var show_comment = ""; var __appmsgCgiData = { wxa_product : ""*1, wxa_cps : ""*1, show_msg_voice: "0"*1, can_use_page : "0"*1, is_wxg_stuff_uin : "0"*1, card_pos : "", copyright_stat : "1", source_biz : "", hd_head_img : "http://wx.qlogo.cn/mmhead/Q3auHgzwzM48ayvz1uibnIzEqzb3z6EgfcN6sJW6L2sTIWUibcpicqWog/0"||(window.location.protocol+"//"+window.location.host + "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/pic/appmsg/pic_rumor_link.2x264e76.jpg") }; var _empty_v = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/pic/pages/voice/empty26f1f1.mp3"; var copyright_stat = "1" * 1; var pay_fee = "" * 1; var pay_timestamp = ""; var need_pay = "" * 1; var need_report_cost = "0" * 1; var use_tx_video_player = "0" * 1; var appmsg_fe_filter = "contenteditable"; var friend_read_source = "" || ""; var friend_read_version = "" || ""; var friend_read_class_id = "" || ""; var is_only_read = "1" * 1; var read_num = "" * 1; var like_num = "" * 1; var liked = "" == 'true' ? true : false; var is_temp_url = "" ? 1 : 0; var send_time = ""; var icon_emotion_switch = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/icon/appmsg/emotion/icon_emotion_switch.2x2f1273.png"; var icon_emotion_switch_active = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/icon/appmsg/emotion/icon_emotion_switch_active.2x2f1273.png"; var icon_loading_white = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/icon/common/icon_loading_white2805ea.gif"; var icon_audio_unread = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/icon/appmsg/audio/icon_audio_unread26f1f1.png"; var icon_qqmusic_default = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/icon/appmsg/qqmusic/icon_qqmusic_default.2x26f1f1.png"; var icon_qqmusic_source = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/icon/appmsg/qqmusic/icon_qqmusic_source393e3a.png"; var icon_kugou_source = "//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/icon/appmsg/kugou/icon_kugou_source393e3a.png"; var topic_default_img = '//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/icon/appmsg/topic/pic_book_thumb.2x2e4987.png'; var comment_edit_icon = '//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/icon/appmsg_new/icon_edit36906d.png'; var comment_loading_img = '//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/icon/common/icon_loading_white2805ea.gif'; var voice_in_appmsg = { "1":"1" }; var reprint_style = ''*1; var wxa_img_alert = "" != 'false'; var weapp_sn_arr_json = "" || ""; var ban_scene = "0" * 1; var svr_time = "1542802155" * 1; var is_transfer_msg = ""*1||0; var malicious_title_reason_id = "0" * 1; var malicious_content_type = "0" * 1; var modify_time = ""; var isprofileblock = "0"; var hotspotInfoList = [ ]; window.wxtoken = "777"; window.is_login = '0' * 1; window.__moon_initcallback = function(){ if(!!window.__initCatch){ window.__initCatch({ idkey : 27611+2, startKey : 0, limit : 128, badjsId: 43, reportOpt : { uin : uin, biz : biz, mid : mid, idx : idx, sn : sn }, extInfo : { network_rate : 0.01, badjs_rate: 0.1 } }); } } var title ="数据派THU"; var is_new_msg=true; window.__moon_host = 'res.wx.qq.com';window.__moon_mainjs = 'appmsg/index.js';window.moon_map = {"a/appdialog_confirm.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/appdialog_confirm.html34f0d8.js","widget/wx_profile_dialog_primary.css":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/widget/wx_profile_dialog_primary.css3de35e.js","pages/iframe_communicate.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/iframe_communicate409b7e.js","new_video/player.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/new_video/player.html410d1c.js","biz_wap/zepto/touch.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/zepto/touch34c264.js","biz_wap/zepto/event.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/zepto/event34c264.js","biz_wap/zepto/zepto.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/zepto/zepto34c264.js","page/pages/video.css":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/page/pages/video.css40526b.js","a/tpl/sponsor_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/tpl/sponsor_tpl.html3fe228.js","a/tpl/new_cpc_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/tpl/new_cpc_tpl.html402b93.js","appmsg/emotion/caret.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/emotion/caret278965.js","biz_wap/utils/localstorage.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/localstorage36c4f2.js","appmsg/friend_comment_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/friend_comment_tpl.html3de35e.js","appmsg/comment_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/comment_tpl.html3de35e.js","biz_wap/utils/fakehash.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/fakehash38c7af.js","appmsg/comment_report.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/comment_report3d5603.js","a/appdialog_confirm.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/appdialog_confirm34c32a.js","new_video/player.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/new_video/player41b4d4.js","a/tpl/crt_size_map.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/tpl/crt_size_map3fe228.js","biz_wap/jsapi/cardticket.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/jsapi/cardticket34c264.js","biz_common/jquery.md5.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/jquery.md53518c6.js","biz_common/utils/emoji_panel_data.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/emoji_panel_data3518c6.js","biz_common/utils/emoji_data.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/emoji_data3518c6.js","appmsg/emotion/textarea.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/emotion/textarea3d171e.js","appmsg/emotion/nav.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/emotion/nav278965.js","appmsg/emotion/common.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/emotion/common3518c6.js","appmsg/emotion/slide.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/emotion/slide2a9cd9.js","appmsg/emotion/dom.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/emotion/dom31ff31.js","biz_common/utils/wxgspeedsdk.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/wxgspeedsdk3518c6.js","pages/music_report_conf.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/music_report_conf3c6d6e.js","pages/report.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/report41b4d4.js","pages/player_adaptor.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/player_adaptor39d6ee.js","pages/music_player.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/music_player3d3b85.js","pages/loadscript.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/loadscript3d3b85.js","biz_wap/utils/ajax_load_js.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/ajax_load_js3d3b85.js","appmsg/comment.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/comment41b040.js","a/video.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/video40bd1e.js","a/ios.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/ios3ff7ef.js","a/android.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/android40038e.js","a/profile.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/profile41b040.js","a/app_card.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/app_card41fe77.js","a/sponsor.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/sponsor409b7e.js","a/tpl/cpc_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/tpl/cpc_tpl.html402b93.js","a/tpl/crt_tpl_manager.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/tpl/crt_tpl_manager41b040.js","a/cpc_a_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/cpc_a_tpl.html3e57e7.js","a/sponsor_a_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/sponsor_a_tpl.html3ec2ed.js","a/a_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/a_tpl.html417d8e.js","a/mpshop.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/mpshop3ff7ef.js","a/wxopen_card.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/wxopen_card41b040.js","a/card.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/card311179.js","biz_wap/utils/position.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/position34c264.js","a/a_report.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/a_report41b040.js","biz_wap/utils/show_time.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/show_time41444b.js","biz_common/utils/get_para_list.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/get_para_list40c31a.js","biz_wap/utils/openUrl.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/openUrl41d825.js","a/a_sign.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/a_sign41b040.js","appmsg/my_comment_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/my_comment_tpl.html41fe77.js","appmsg/cmt_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/cmt_tpl.html3de35e.js","sougou/a_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/sougou/a_tpl.html2c6e7c.js","appmsg/emotion/emotion.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/emotion/emotion3f5bf4.js","biz_common/base64.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/base643518c6.js","biz_wap/utils/wapsdk.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/wapsdk34c264.js","biz_common/utils/report.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/report3518c6.js","appmsg/articleReport.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/articleReport3de35e.js","biz_wap/utils/hand_up_state.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/hand_up_state3e72f4.js","biz_common/utils/http.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/http3518c6.js","biz_common/utils/cookie.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/cookie3518c6.js","appmsg/topic_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/topic_tpl.html31ff31.js","pages/weapp_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/weapp_tpl.html36906d.js","biz_common/utils/monitor.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/monitor3518c6.js","pages/voice_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/voice_tpl.html38518d.js","pages/kugoumusic_ctrl.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/kugoumusic_ctrl3d171e.js","pages/qqmusic_ctrl.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/qqmusic_ctrl3d171e.js","pages/voice_component.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/voice_component406e84.js","pages/qqmusic_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/qqmusic_tpl.html393e3a.js","new_video/ctl.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/new_video/ctl41b4d4.js","appmsg/open_url_with_webview.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/open_url_with_webview3d3b85.js","a/testdata.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/testdata3bb523.js","appmsg/reward_entry.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/reward_entry3ebded.js","appmsg/like.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/like3d171e.js","pages/version4video.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/version4video40c91a.js","biz_wap/utils/storage.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/storage34c264.js","appmsg/share_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/share_tpl.html36906d.js","appmsg/appmsgext.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/appmsgext41b040.js","appmsg/img_copyright_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/img_copyright_tpl.html2a2c13.js","pages/video_ctrl.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/video_ctrl4047ec.js","pages/create_txv.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/create_txv401e7e.js","appmsg/comment_utils.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/comment_utils41b040.js","biz_common/ui/imgonepx.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/ui/imgonepx3518c6.js","appmsg/malicious_wording.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/malicious_wording3dd66a.js","a/a.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/a41fe77.js","rt/appmsg/getappmsgext.rt.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/rt/appmsg/getappmsgext.rt2c21f6.js","pages/video_communicate_adaptor.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/pages/video_communicate_adaptor41b4d4.js","biz_wap/utils/ajax_wx.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/ajax_wx405300.js","biz_common/utils/respTypes.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/respTypes3518c6.js","biz_wap/utils/log.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/log34c264.js","sougou/index.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/sougou/index41fe77.js","biz_wap/safe/mutation_observer_report.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/safe/mutation_observer_report34c264.js","appmsg/fereport.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/fereport410774.js","appmsg/fereport_without_localstorage.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/fereport_without_localstorage413fb1.js","appmsg/report.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/report3d6a60.js","appmsg/report_and_source.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/report_and_source407ef0.js","appmsg/page_pos.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/page_pos4137de.js","appmsg/cdn_speed_report.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/cdn_speed_report3d6a60.js","appmsg/wxtopic.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/wxtopic31a3be.js","appmsg/weapp.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/weapp3ff8fe.js","appmsg/weproduct.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/weproduct3f868a.js","appmsg/voicemsg.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/voicemsg3b1748.js","appmsg/autoread.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/autoread3af14e.js","appmsg/voice.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/voice38518d.js","appmsg/qqmusic.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/qqmusic3d171e.js","appmsg/iframe.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/iframe41b4d4.js","appmsg/product.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/product393966.js","appmsg/review_image.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/review_image41b4d4.js","appmsg/outer_link.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/outer_link3e0906.js","appmsg/copyright_report.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/copyright_report2ec4b2.js","appmsg/async.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/async41b040.js","biz_wap/ui/lazyload_img.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/ui/lazyload_img4137de.js","biz_common/log/jserr.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/log/jserr3518c6.js","appmsg/share.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/share41caca.js","appmsg/cdn_img_lib.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/cdn_img_lib41931e.js","appmsg/finance_communicate.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/finance_communicate41f711.js","page/appmsg_new/not_in_mm.css":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/page/appmsg_new/not_in_mm.css3ec991.js","page/appmsg_new/combo.css":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/style/page/appmsg_new/combo.css41834c.js","a/mpAdAsync.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/a/mpAdAsync41b040.js","biz_common/utils/url/parse.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/url/parse36ebcf.js","appmsg/appmsg_report.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/appmsg_report41b4d4.js","biz_common/moment.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/moment3518c6.js","biz_wap/jsapi/core.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/jsapi/core3d3b85.js","biz_common/dom/event.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/dom/event3a25e9.js","appmsg/test.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/test3d3b85.js","biz_wap/utils/mmversion.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/mmversion3de208.js","appmsg/max_age.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/max_age3d3b85.js","biz_common/dom/attr.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/dom/attr3518c6.js","biz_wap/utils/ajax.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/ajax405300.js","appmsg/log.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/log300330.js","biz_common/dom/class.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/dom/class3518c6.js","biz_wap/utils/device.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_wap/utils/device3e2bd7.js","appmsg/weapp_common.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/weapp_common41b040.js","biz_common/utils/string/html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/utils/string/html410c03.js","cps/tpl/list_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/cps/tpl/list_tpl.html3f868a.js","cps/tpl/card_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/cps/tpl/card_tpl.html3f868a.js","cps/tpl/banner_tpl.html.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/cps/tpl/banner_tpl.html3f868a.js","biz_common/tmpl.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/biz_common/tmpl3518c6.js","appmsg/index.js":"//res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/appmsg/index41fe77.js"}; r;++r)if(o.call(t,e[r],r,e)===!1)return; }else{ if("Object"!==n&&a!=e)throw"unsupport type"; if(a==e){ for(var r=e.length-1;r>=0;r--){ var s=a.key(r),c=a.getItem(s); if(o.call(t,c,s,e)===!1)return; } return; } for(var r in e)if(e.hasOwnProperty(r)&&o.call(t,e[r],r,e)===!1)return; } } } var a=o.localStorage,s=document.head||document.getElementsByTagName("head")[0],c=1,d=11,_=12,m=13,l=window.__allowLoadResFromMp?1:2,w=window.__allowLoadResFromMp?1:0,u=l+w,p=window.testenv_reshost||window.__moon_host||"res.wx.qq.com",f=new RegExp("^(http(s)?:)?//"+p); window.__loadAllResFromMp&&(p="mp.weixin.qq.com",l=0,u=l+w); var h={ prefix:"__MOON__", loaded:[], unload:[], clearSample:!0, hit_num:0, mod_num:0, version:1003, cacheData:{ js_mod_num:0, js_hit_num:0, js_not_hit_num:0, js_expired_num:0, css_mod_num:0, css_hit_num:0, css_not_hit_num:0, css_expired_num:0 }, init:function(){ h.loaded=[],h.unload=[]; var e,t,r; if(window.no_moon_ls&&(h.clearSample=!0),a){ var s="_moon_ver_key_",c=a.getItem(s); c!=h.version&&(h.clear(),a.setItem(s,h.version)); } if((-1!=location.search.indexOf("no_moon1=1")||-1!=location.search.indexOf("no_lshttps=1"))&&h.clear(), a){ var d=1*a.getItem(h.prefix+"clean_time"),_=+new Date; if(_-d>=1296e6){ h.clear(); try{ !!a&&a.setItem(h.prefix+"clean_time",+new Date); }catch(m){} } } i(moon_map,function(i,s){ if(t=h.prefix+s,r=!!i&&i.replace(f,""),e=!!a&&a.getItem(t),version=!!a&&(a.getItem(t+"_ver")||"").replace(f,""), h.mod_num++,r&&-1!=r.indexOf(".css")?h.cacheData.css_mod_num++:r&&-1!=r.indexOf(".js")&&h.cacheData.js_mod_num++, h.clearSample||!e||r!=version)h.unload.push(r.replace(f,"")),r&&-1!=r.indexOf(".css")?e?r!=version&&h.cacheData.css_expired_num++:h.cacheData.css_not_hit_num++:r&&-1!=r.indexOf(".js")&&(e?r!=version&&h.cacheData.js_expired_num++:h.cacheData.js_not_hit_num++);else{ if("https:"==location.protocol&&window.moon_hash_map&&window.moon_hash_map[s]&&window.crypto)try{ n(e).then(function(e){ window.moon_hash_map[s]!=e&&console.log(s); }); }catch(c){} try{ var d="//# sourceURL="+s+"\n//@ sourceURL="+s; o.eval.call(o,'define("'+s+'",[],'+e+")"+d),h.hit_num++,r&&-1!=r.indexOf(".css")?h.cacheData.css_hit_num++:r&&-1!=r.indexOf(".js")&&h.cacheData.js_hit_num++; }catch(c){ h.unload.push(r.replace(f,"")); } } }),h.load(h.genUrl()); }, genUrl:function(){ var e=h.unload; if(!e||e.length<=0)return[]; var o,t,n="",r=[],i={},a=-1!=location.search.indexOf("no_moon2=1"),s="//"+p; -1!=location.href.indexOf("moon_debug2=1")&&(s="//mp.weixin.qq.com"); for(var c=0,d=e.length;d>c;++c){ /^\/(.*?)\//.test(e[c]); var _=/^\/(.*?)\//.exec(e[c]); _.length<2||!_[1]||(t=_[1],n=i[t],n?(o=n+","+e[c],o.length>1e3||a?(r.push(n+"?v="+h.version), n=location.protocol+s+e[c],i[t]=n):(n=o,i[t]=n)):(n=location.protocol+s+e[c],i[t]=n)); } for(var m in i)i.hasOwnProperty(m)&&r.push(i[m]); return r; }, load:function(e){ if(window.__wxgspeeds&&(window.__wxgspeeds.mod_num=h.mod_num,window.__wxgspeeds.hit_num=h.hit_num), !e||e.length<=0)return seajs.combo_status=seajs.COMBO_LOADED,seajs.run(),console.debug&&console.debug("[moon] load js complete, all in cache, cost time : 0ms, total count : "+h.mod_num+", hit num: "+h.hit_num), void window.__moonclientlog.push("[moon] load js complete, all in cache, cost time : 0ms, total count : "+h.mod_num+", hit num: "+h.hit_num); seajs.combo_status=seajs.COMBO_LOADING; var o=0,t=+new Date; window.__wxgspeeds&&(window.__wxgspeeds.combo_times=[],window.__wxgspeeds.combo_times.push(t)), i(e,function(n){ h.request(n,u,function(){ if(window.__wxgspeeds&&window.__wxgspeeds.combo_times.push(+new Date),o++,o==e.length){ var n=+new Date-t; window.__wxgspeeds&&(window.__wxgspeeds.mod_downloadtime=n),seajs.combo_status=seajs.COMBO_LOADED, seajs.run(),console.debug&&console.debug("[moon] load js complete, url num : "+e.length+", total mod count : "+h.mod_num+", hit num: "+h.hit_num+", use time : "+n+"ms"), window.__moonclientlog.push("[moon] load js complete, url num : "+e.length+", total mod count : "+h.mod_num+", hit num: "+h.hit_num+", use time : "+n+"ms"); } }); }); }, request:function(o,n,r){ if(o){ n=n||0,o.indexOf("mp.weixin.qq.com")>-1&&((new Image).src=location.protocol+"//mp.weixin.qq.com/mp/jsmonitor?idkey=27613_32_1&r="+Math.random(), window.__moon_report([{ offset:_, log:"load_script_from_mp: "+o }],1)); var i=-1; window.__DEBUGINFO&&(__DEBUGINFO.res_list||(__DEBUGINFO.res_list=[]),__DEBUGINFO.res_list.push({ type:"js", status:"pendding", start:+new Date, end:0, url:o }),i=__DEBUGINFO.res_list.length-1),-1!=location.search.indexOf("no_lshttps=1")&&(o=o.replace("http://","https://")); var a=document.createElement("script"); a.src=o,a.type="text/javascript",a.async=!0,a.down_time=+new Date,a.onerror=function(s){ t(i,"status","error"),t(i,"end",+new Date); var _=new Error(s); if(n>=0)if(w>n){ var l=o.replace("res.wx.qq.com","mp.weixin.qq.com"); h.request(l,n,r); }else h.request(o,n,r);else window.__moon_report&&window.__moon_report([{ offset:c, log:"load_script_error: "+o, e:_ }],1); if(n==w-1&&window.__moon_report([{ offset:d, log:"load_script_error: "+o, e:_ }],1),-1==n){ var u="ua: "+window.navigator.userAgent+", time="+(+new Date-a.down_time)+", load_script_error -1 : "+o; window.__moon_report([{ offset:m, log:u }],1); } window.__moonclientlog.push("moon load js error : "+o+", error -> "+_.toString()), e("moon_request_error url:"+o); },"undefined"!=typeof moon_crossorigin&&moon_crossorigin&&a.setAttribute("crossorigin",!0), a.onload=a.onreadystatechange=function(){ t(i,"status","loaded"),t(i,"end",+new Date),!a||a.readyState&&!/loaded|complete/.test(a.readyState)||(t(i,"status","200"), a.onload=a.onreadystatechange=null,"function"==typeof r&&r()); },n--,s.appendChild(a),e("moon_request url:"+o+" retry:"+n); } }, setItem:function(e,o){ !!a&&a.setItem(e,o); }, clear:function(){ a&&(i(a,function(e,o){ ~o.indexOf(h.prefix)&&a.removeItem(o); }),console.debug&&console.debug("[moon] clear")); }, idkeyReport:function(e,o,t){ t=t||1; var n=e+"_"+o+"_"+t; (new Image).src="/mp/jsmonitor?idkey="+n+"&r="+Math.random(); } }; seajs&&seajs.use&&"string"==typeof window.__moon_mainjs&&seajs.use(window.__moon_mainjs), window.moon=h; }(window),function(){ try{ Math.random()<1; }catch(e){} }(),window.moon.init(); }; e(),!!window.__moon_initcallback&&window.__moon_initcallback(),window.__wxgspeeds&&(window.__wxgspeeds.moonendtime=+new Date); } } __moonf__(); }, 25);

    RankCompanyLocationYear endAnnual sales rise over 3 yearsLatest sales £000sStaffComment
    1WonderblyPersonalised children's booksEast LondonApr-17294.27%*25,86080Has sold nearly 3m customisable children’s books in 200 countries
     元素来写入csv或JSON。


    循环遍历元素并保存变量


    在Python中,将结果附加到一个列表中是很有用的,然后将数据写到一个文件中。我们应该在循环之前声明列表并设置csv的头文件,如下所示:


    # create and write headers to a list 
    rows = []
    rows.append(['Rank', 'Company Name', 'Webpage', 'Description', 'Location', 'Year end', 'Annual sales rise over 3 years', 'Sales £000s', 'Staff', 'Comments'])
    print(rows)


    这将打印出我们添加到包含标题的列表的第一行。


    你可能会注意到表格中有一些额外的字段Webpage和Description不是列名,但是如果你仔细看看我们打印上面的soup变量时的html,那么第二行不仅仅包含公司名称。我们可以使用一些进一步的提取来获取这些额外信息。


    下一步是循环结果,处理数据并附加到可以写入csv的rows。


    在循环中查找结果:


    # loop over results
    for result in results:
        # find all columns per result
        data = result.find_all('td')
        # check that columns have data 
        if len(data) == 0: 
            continue


    由于表中的第一行仅包含标题,因此我们可以跳过此结果,如上所示。它也不包含任何

    元素,因此在搜索元素时,不会返回任何内容。然后,我们可以通过要求数据的长度为非零来检查是否只处理包含数据的结果。


    然后我们可以开始处理数据并保存到变量中。


        # write columns to variables
        rank = data[0].getText()
        company = data[1].getText()
        location = data[2].getText()
        yearend = data[3].getText()
        salesrise = data[4].getText()
        sales = data[5].getText()
        staff = data[6].getText()
        comments = data[7].getText()


    以上只是从每个列获取文本并保存到变量。但是,其中一些数据需要进一步清理以删除不需要的字符或提取更多信息。


    数据清理


    如果我们打印出变量company,该文本不仅包含公司名称,还包含描述。我们然后打印sales,它包含不需要的字符,如脚注符号,最好删除。


        print('Company is', company)
        # Company is WonderblyPersonalised children's books          
        print('Sales', sales)
        # Sales *25,860


    我们希望将company 分为公司名称和描述,我们可以用几行代码实现。再看一下html,对于这个列,有一个  元素只包含公司名称。此列中还有一个链接指向网站上的另一个页面,其中包含有关该公司的更多详细信息。我们将在稍后使用它!


    WonderblyPersonalised children's books


    今天看啥 - 高品质阅读平台
    本文地址:http://www.jintiankansha.me/t/Lr5RhlEJUX
    Python社区是高质量的Python/Django开发社区
    本文地址:http://www.python88.com/topic/26651
     
    1056 次点击