书接上回,我们获取到了使用动态加载技术加载的新闻链接,但是,有些不是动态加载的新闻链接,我们还没有处理。我们将doc类型文档拷贝下来,放入HBuilder X编辑器中,然后打开内置浏览器,打开开发者工具。
这里不直接使用chrome定位元素,因为chrome看到的html实际上不是我们这个请求获取到的代码。
我们定位到了一个 div 它子元素 是一个 div 和5个无序列表,显然,这就是我们要找的。仔细观察,我们发现新闻链接 a 标签都在 li 标签中。
很好,那我们就有思路了。
- 模拟请求,获取相应。
- 取出 所有的 li 标签下的 a 标签。
- a 标签的href 属性就是我们要的新闻链接
“Talk is cheap,give me code.”
import requests
import lxml.html
url = 'http://news.baidu.com/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Referer': 'http://news.baidu.com/guoji',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': 'BAIDUID=BB2DCB09D0BCEF14D99F6A6A6CE5A1DB:FG=1; BIDUPSID=BB2DCB09D0BCEF14D99F6A6A6CE5A1DB; PSTM=1549988189; BDUSS=H40YlBUdE5FaklweFFvc2szYmpILWRTdGlGeTlPRUxYZ3hPdVctQXVDYWVmelZkSVFBQUFBJCQAAAAAAAAAAAEAAAAVmE-ksKLA78K3ybXJtQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJ7yDV2e8g1dd; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=1450_21092_29522_29518_29098_29568_28831_29221_22159; BDSFRCVID=M_0sJeCCxG3jeU3w_mtxLaFULTy8O-bEthyW3J; H_BDCLCKID_SF=tbkD_C-MfIvjHn8z2KT8bKCShUFsK6jW-2Q-5hOy3KO8eqRv5pnGX4P-KHrf3x6dtNLqL-L2MPOvhpFuDTtajj3QeaRt2tcyatj2WnTJ25r8e5rnhPF3yftTKP6-3MJO3b7B0l7FbM7G8h6bMf8By58dyajCQlkHaI5mohFLtDKhbKKCj5RMK4_SMUoHetrK-D5XQbC8Kb7VbpTEDMnkbfJBDGJR5-Pq-jvdblRhBCQIjMnTyURdXlD7yajKBlvWWmbx2Ron3RQTjnOqKPTpQT8r5b_OK5Oib4j-KbONab3vOpRzXpO1KMPzBN5thURB2DkO-4bCWJ5TMl5jDh05y6TXDN0qtTteJb3fL-08MJnEqbTkq4bohjPyX-jeBtQm05bxobLyJD5b_n6Ljn8MMpkS-P5n0jcqbHrDs4PbWDFKMC_GjjthDjPVMmTt2tc2K6r-04_8Kb7VbIJCXMnkbftWXfvmQ-oI-jvdWx5n2t5ZSfnueMri5bK7yajK2hTJbD3MVlrh3bvsVCbPQJ7pQT8ryhAOK5OibCrk2b5oab3vOpRzXpO1KMPzBN5thURB2DkO-4bCWJ5TMl5jDh05y6TLeHLDtTkqtR3tWJTOaj6jDbTnMIT8bKCShUFsy5cJB2Q-5KL--qoZeJ4G5pnVMMtq5JrPXPnpJCczafbdJJjoh4Pl-to2-fD7jnQHKTQdBgTxoUJh5DnJhhvG-xAb54LebPRiWPr9QgbjahQ7tt5W8ncFbT7l5hKpbt-q0x-jLn7ZVDD5fCtMMItr5b5H-PQHjHQB2-DXKKOLVh67tPOkeqOJ2Mt5M4LADpJd2R3OBI6l-lRmMCncKJoH5-7B3TtpexbH55uqJR4JVU5; Hm_lvt_e9e114d958ea263de46e080563e254c4=1565684960; LOCALGX=%u957F%u6C99%7C%35%31%36%32%7C%u957F%u6C99%7C%35%31%36%32; delPer=0; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=3; Hm_lpvt_e9e114d958ea263de46e080563e254c4=1565742579',
}
response = requests.get(url=url,headers=headers)
html_tree = lxml.html.etree.HTML(response.text) # 生成xpath解析对象
items = html_tree.xpath('//div[@id="body"]//li//a/@href') # //选取所有元素 /选取子元素或属性
输出看看:
['javascript:void(0);',
'http://m.news.cctv.com/2019/08/14/ARTIgmYd33BmyNsrPXVRKRg1190814.shtml',
'http://opinion.cctv.com/2019/08/14/ARTI36SgnScAujW6lbntQdGs190814.shtml',
'http://opinion.people.com.cn/n1/2019/0813/c1003-31292590.html',
'http://world.people.com.cn/n1/2019/0814/c1002-31293368.html',
'http://www.xinhuanet.com/politics/2019-08/13/c_1124871587.htm',
'http://www.xinhuanet.com/fortune/2019-08/13/c_1124871897.htm',
'http://china.cnr.cn/news/20190813/t20190813_524728803.shtml',
'http://www.xinhuanet.com/fortune/2019-08/13/c_1124870637.htm',
'https://www.thepaper.cn/newsDetail_forward_4151939',
'http://jingji.cctv.com/2019/08/14/ARTIcLXLCXdEl98Bzp4dP9og190814.shtml',
'http://www.xinhuanet.com/comments/2019-08/13/c_1124871201.htm',
'http://china.cnr.cn/NewsFeeds/20190812/t20190812_524728626.shtml',
'https://3w.huanqiu.com/a/564394/7P7MuFVkGt2?agt=8',
'https://3w.huanqiu.com/a/94d2e8/7P86kVZnYWc?agt=8',
'https://3w.huanqiu.com/a/86df83/7P87mvVZJew?agt=8',
'https://3w.huanqiu.com/a/9e85a6/7P8fZIooI9y?agt=8',
'https://3w.huanqiu.com/a/c36dc8/7P8b1UHRFYc?agt=8',
'https://3w.huanqiu.com/a/0c789f/7P7Z4ctSdji?agt=8',
'https://3w.huanqiu.com/a/a6b8af/7P8gKAKd5XG?agt=8',
'http://baijiahao.baidu.com/s?id=1641814751754264937',
'https://3w.huanqiu.com/a/a4d1ef/7P8kMc4M6zK?agt=8',
'https://3w.huanqiu.com/a/a4d1ef/7P7Rlc286w8?agt=8',
'https://3w.huanqiu.com/a/21eee3/7P7VvNLF1pC?agt=8',
'https://3w.huanqiu.com/a/c36dc8/7P86phvKNHO?agt=8',
'https://3w.huanqiu.com/a/c36dc8/7P7ZioA8SK4?agt=8',
'https://3w.huanqiu.com/a/4e1ccd/7P8jklw8Ffq?agt=8',
'https://3w.huanqiu.com/a/a4d1ef/7P7MT8a7jLG?agt=8',
'https://3w.huanqiu.com/a/a4d1ef/7P82aNn6Nvq?agt=8',
'https://3w.huanqiu.com/a/3458fa/7P885yABln2?agt=8',
'http://baijiahao.baidu.com/s?id=1641819009062212746',
'http://baijiahao.baidu.com/s?id=1641808311082531117',
'https://3w.huanqiu.com/a/0c789f/7P8dUnucNO0?agt=',
'https://3w.huanqiu.com/a/c4b13d/7P7LZd6Ak5q?agt=8',
'http://baijiahao.baidu.com/s?id=1641813846398704393',
'http://baijiahao.baidu.com/s?id=1641820893074131143',
'https://3w.huanqiu.com/a/c4b13d/7P7St8kvInS?agt=8',
'https://3w.huanqiu.com/a/4d0bf0/7P7Q7eIGCJi?agt=8',
'https://3w.huanqiu.com/a/8737f0/7P8f5hFsmXe?agt=8',
'https://3w.huanqiu.com/a/9029b5/7P8hdSTgYpi?agt=8',
'https://www.baidu.com/s?wd=%E4%B9%A0%E8%BF%91%E5%B9%B3%E6%80%BB%E4%B9%A6%E8%AE%B0%E7%89%B5%E6%8C%82%E7%9A%84%E6%B0%91%E7%94%9F%E4%BA%8B',
'https://www.baidu.com/s?wd=%E6%B7%B1%E5%9C%B3600%E6%9E%B6%E6%97%A0%E4%BA%BA%E6%9C%BA%E5%BA%94%E6%8F%B4%E9%A6%99%E6%B8%AF',
'https://www.baidu.com/s?wd=5G%E6%96%B0%E5%8F%B7%E6%AE%B5%E4%B8%8B%E6%9C%88%E6%94%BE%E5%8F%B7',
'https://www.baidu.com/s?wd=%E6%B8%AF%E8%AD%A6%E8%B0%B4%E8%B4%A3%E6%9A%B4%E5%BE%92',
'https://www.baidu.com/s?wd=%E5%9B%BD%E6%B3%B0%E8%88%AA%E7%A9%BA%E8%82%A1%E4%BB%B7%E5%A4%A7%E8%B7%8C',
'https://www.baidu.com/s?wd=%E5%93%AA%E5%90%92%E7%A5%A8%E6%88%BF%E8%B6%85%E7%BA%A2%E6%B5%B7%E8%A1%8C%E5%8A%A8',
'https://www.baidu.com/s?wd=%E6%97%A5%E6%9C%AC%E7%A7%BB%E5%87%BA%E7%99%BD%E5%90%8D%E5%8D%95',
'https://www.baidu.com/s?wd=%E6%BB%B4%E6%BB%B4%E8%A2%AB%E7%BD%9A550%E4%B8%87%E5%85%83',
'https://www.baidu.com/s?wd=%E9%A6%99%E6%B8%AF%E8%AD%A6%E9%98%9F%E7%94%B7%E8%B6%B3%E5%A4%BA%E5%86%A0',
'https://www.baidu.com/s?wd=%E9%9D%92%E5%B2%9B%E5%B8%82%E6%B0%91%E6%8D%A1%E6%B5%B7%E9%B2%9C',
'http://baijiahao.baidu.com/s?id=1641806920856311184',
'http://baijiahao.baidu.com/s?id=1641801134242707690',
'http://baijiahao.baidu.com/s?id=1641804985134705772',
'http://baijiahao.baidu.com/s?id=1641795161667151424',
'http://baijiahao.baidu.com/s?id=1641759706552362036',
'http://baijiahao.baidu.com/s?id=1641770098855674130',
'http://baijiahao.baidu.com/s?id=1641792566833082373',
'http://baijiahao.baidu.com/s?id=1641794233672307058',
'https://baijiahao.baidu.com/s?id=1641800725680668397&wfr=content',
'http://baijiahao.baidu.com/s?id=1641808179460287460',
'http://baijiahao.baidu.com/s?id=1641811388867477686',
'http://baijiahao.baidu.com/s?id=1641811286045373068',
'http://baijiahao.baidu.com/s?id=1641805465118766336',
'http://baijiahao.baidu.com/s?id=1641809855939168928',
'http://baijiahao.baidu.com/s?id=1641824412404067709',
'http://baijiahao.baidu.com/s?id=1641673081149173815',
'http://report.12377.cn:13225/toreportinputNormal_anis.do',
'javascript:void(0);',
'javascript:void(0);',
'javascript:void(0);',
'javascript:void(0);',
'javascript:void(0);']
嗯,结果不错,我们只要将不是网址的和百度搜索的数据剔除掉就可以了。
经过整理,我们发现百度新闻主要有百家号、央视网和环球新闻三个媒体的新闻,百家号占比较多,所以,我们先对百家号入手。
我们选取这条新闻,和上面一样,打开开发者工具,刷新一下。
很好,看来新闻就是一个doc了,我们使用inspect工具
我们很快定位到了新闻的标题和正文。
上代码:
import requests
import lxml.html
url = 'http://baijiahao.baidu.com/s?id=1641674895594803957'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Referer': 'http://news.baidu.com/guoji',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': 'BAIDUID=BB2DCB09D0BCEF14D99F6A6A6CE5A1DB:FG=1; BIDUPSID=BB2DCB09D0BCEF14D99F6A6A6CE5A1DB; PSTM=1549988189; BDUSS=H40YlBUdE5FaklweFFvc2szYmpILWRTdGlGeTlPRUxYZ3hPdVctQXVDYWVmelZkSVFBQUFBJCQAAAAAAAAAAAEAAAAVmE-ksKLA78K3ybXJtQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJ7yDV2e8g1dd; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=1450_21092_29522_29518_29098_29568_28831_29221_22159; BDSFRCVID=M_0sJeCCxG3jeU3w_mtxLaFULTy8O-bEthyW3J; H_BDCLCKID_SF=tbkD_C-MfIvjHn8z2KT8bKCShUFsK6jW-2Q-5hOy3KO8eqRv5pnGX4P-KHrf3x6dtNLqL-L2MPOvhpFuDTtajj3QeaRt2tcyatj2WnTJ25r8e5rnhPF3yftTKP6-3MJO3b7B0l7FbM7G8h6bMf8By58dyajCQlkHaI5mohFLtDKhbKKCj5RMK4_SMUoHetrK-D5XQbC8Kb7VbpTEDMnkbfJBDGJR5-Pq-jvdblRhBCQIjMnTyURdXlD7yajKBlvWWmbx2Ron3RQTjnOqKPTpQT8r5b_OK5Oib4j-KbONab3vOpRzXpO1KMPzBN5thURB2DkO-4bCWJ5TMl5jDh05y6TXDN0qtTteJb3fL-08MJnEqbTkq4bohjPyX-jeBtQm05bxobLyJD5b_n6Ljn8MMpkS-P5n0jcqbHrDs4PbWDFKMC_GjjthDjPVMmTt2tc2K6r-04_8Kb7VbIJCXMnkbftWXfvmQ-oI-jvdWx5n2t5ZSfnueMri5bK7yajK2hTJbD3MVlrh3bvsVCbPQJ7pQT8ryhAOK5OibCrk2b5oab3vOpRzXpO1KMPzBN5thURB2DkO-4bCWJ5TMl5jDh05y6TLeHLDtTkqtR3tWJTOaj6jDbTnMIT8bKCShUFsy5cJB2Q-5KL--qoZeJ4G5pnVMMtq5JrPXPnpJCczafbdJJjoh4Pl-to2-fD7jnQHKTQdBgTxoUJh5DnJhhvG-xAb54LebPRiWPr9QgbjahQ7tt5W8ncFbT7l5hKpbt-q0x-jLn7ZVDD5fCtMMItr5b5H-PQHjHQB2-DXKKOLVh67tPOkeqOJ2Mt5M4LADpJd2R3OBI6l-lRmMCncKJoH5-7B3TtpexbH55uqJR4JVU5; Hm_lvt_e9e114d958ea263de46e080563e254c4=1565684960; LOCALGX=%u957F%u6C99%7C%35%31%36%32%7C%u957F%u6C99%7C%35%31%36%32; delPer=0; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=3; Hm_lpvt_e9e114d958ea263de46e080563e254c4=1565742579',
}
response = requests.get(url=url,headers=headers)
html_tree = lxml.html.etree.HTML(response.text) # 生成xpath解析对象
news_info = {}
news_info['title'] = html_tree.xpath('//div[@class="article-title"][1]/h2/text()') [0] # //选取所有元素 /选取子元素或属性
news_info['content'] = html_tree.xpath('//div[@id="article"]//text()')
我创建了一个字典来存储新闻内容,打印看看:
{'title': ['受召回计划影响 蔚来ES8 7月交付量同比下滑67%'],
'content': '8月12日晚间,蔚来汽车公布今年7月的汽车交付数据,其向美国证券交易委员会递交的文件显示,7月期间蔚来ES8交付量同比下滑67%。受此影响,蔚来开盘后一度跌超5%,其后股价企稳回升。截至发稿时止,蔚来股价报3.06美元/ADS,下跌2.08%。蔚来公布的数据显示,7月份共交付837辆新车,环比跌约37.5%。其中,ES8交付数量为164辆,同比下滑67%;ES6已交付673辆。蔚来创始人李斌在公告中表示,7月份交付数量低迷的主要原因是公司主动召回4803辆ES8,导致电池供应紧张,影响了7月的生产和交付。'}
import requests
import lxml.html
url = 'http://news.cctv.com/2019/07/31/ARTIDo8zXI4hAW3U7FflP1vD190731.shtml?spm=C94212.PxBacxQyDqwK.S95581.14'
headers = {
'User-Agent': 'Mozillaa/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Referer': 'http://news.baidu.com/guoji',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': 'BAIDUID=BB2DCB09D0BCEF14D99F6A6A6CE5A1DB:FG=1; BIDUPSID=BB2DCB09D0BCEF14D99F6A6A6CE5A1DB; PSTM=1549988189; BDUSS=H40YlBUdE5FaklweFFvc2szYmpILWRTdGlGeTlPRUxYZ3hPdVctQXVDYWVmelZkSVFBQUFBJCQAAAAAAAAAAAEAAAAVmE-ksKLA78K3ybXJtQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJ7yDV2e8g1dd; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=1450_21092_29522_29518_29098_29568_28831_29221_22159; BDSFRCVID=M_0sJeCCxG3jeU3w_mtxLaFULTy8O-bEthyW3J; H_BDCLCKID_SF=tbkD_C-MfIvjHn8z2KT8bKCShUFsK6jW-2Q-5hOy3KO8eqRv5pnGX4P-KHrf3x6dtNLqL-L2MPOvhpFuDTtajj3QeaRt2tcyatj2WnTJ25r8e5rnhPF3yftTKP6-3MJO3b7B0l7FbM7G8h6bMf8By58dyajCQlkHaI5mohFLtDKhbKKCj5RMK4_SMUoHetrK-D5XQbC8Kb7VbpTEDMnkbfJBDGJR5-Pq-jvdblRhBCQIjMnTyURdXlD7yajKBlvWWmbx2Ron3RQTjnOqKPTpQT8r5b_OK5Oib4j-KbONab3vOpRzXpO1KMPzBN5thURB2DkO-4bCWJ5TMl5jDh05y6TXDN0qtTteJb3fL-08MJnEqbTkq4bohjPyX-jeBtQm05bxobLyJD5b_n6Ljn8MMpkS-P5n0jcqbHrDs4PbWDFKMC_GjjthDjPVMmTt2tc2K6r-04_8Kb7VbIJCXMnkbftWXfvmQ-oI-jvdWx5n2t5ZSfnueMri5bK7yajK2hTJbD3MVlrh3bvsVCbPQJ7pQT8ryhAOK5OibCrk2b5oab3vOpRzXpO1KMPzBN5thURB2DkO-4bCWJ5TMl5jDh05y6TLeHLDtTkqtR3tWJTOaj6jDbTnMIT8bKCShUFsy5cJB2Q-5KL--qoZeJ4G5pnVMMtq5JrPXPnpJCczafbdJJjoh4Pl-to2-fD7jnQHKTQdBgTxoUJh5DnJhhvG-xAb54LebPRiWPr9QgbjahQ7tt5W8ncFbT7l5hKpbt-q0x-jLn7ZVDD5fCtMMItr5b5H-PQHjHQB2-DXKKOLVh67tPOkeqOJ2Mt5M4LADpJd2R3OBI6l-lRmMCncKJoH5-7B3TtpexbH55uqJR4JVU5; Hm_lvt_e9e114d958ea263de46e080563e254c4=1565684960; LOCALGX=%u957F%u6C99%7C%35%31%36%32%7C%u957F%u6C99%7C%35%31%36%32; delPer=0; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=3; Hm_lpvt_e9e114d958ea263de46e080563e254c4=1565742579',
}
response = requests.get(url=url,headers=headers)
response.encoding = 'utf-8' # 解决乱码
html_tree = lxml.html.etree.HTML(response.text) # 生成xpath解析对象
news_info = {}
news_info['title'] = html_tree.xpath('//div[@class="cnt_bd"][1]/h1/text()')[0] # //选取所有元素 /选取子元素或属性
news_info['content'] = ''.join(html_tree.xpath('//div[@class="cnt_bd"][1]/p/text()'))
输出看看:
{'title': '国内油价调整搁浅与上调预期并存',
'content': '\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\u3000\u3000记者从多家社会监测机构了解到,近期多空因素交织,国际原油市场呈现震荡回暖,8月6日国内油价新一轮调整窗口将开启,从当前的原油变化率来看,届时搁浅与小幅上调预期并存。\u3000\u3000过去的一周,中东地区紧张局势再度升温,市场担忧原油供应安全恐将受到威胁,提振原油回涨。与此同时,目前美国处于夏季用油高峰期,原油库存大幅下降,但经济疲软的担忧仍存,这些因素相互交织冲抵,国际油价延续宽幅震荡走势,期间出现小幅上涨行情。\u3000\u3000截至7月29日收盘,纽约商品交易所9月交货的轻质原油期货价格上涨0.67美元,收于每桶56.87美元,涨幅为1.19%。9月交货的伦敦布伦特原油期货价格上涨0.25美元,收于每桶63.71美元,涨幅为0.39%。\u3000\u3000受此影响,国内参考的原油变化率仍稳居负值范围,但跌幅不断收窄。据卓创资讯测算,截至7月29日收盘,国内第5个工作日参考原油变化率为-1.31%,对应汽柴油下调49元/吨。而金联创测算的原油变化率在-1.88%,对应的汽柴油应下调50元/吨。\u3000\u3000卓创资讯分析师杨霞指出,虽然目前来看国内成品油零售限价调整幅度不足50元/吨的调整红线,不过因距离调价窗口尚有五个工作日,后期来看,国际原油方面,伊朗形势错综复杂,但各方均保持忍让,事态并未进一步发展但风险犹存;一系列经济数据疲软,多国央行先于美联储降息支撑经济继续扩张。市场缺乏主流影响力,国际油价宽幅震荡区间整理,期间不排除出现上行情况的突破。故原油变化率有望由负转正,本轮成品油零售限价调整搁浅与上调预期并存。当前国内部分地区加油站优惠幅度仍较大,对于广大车主来说,时刻关注当地的加油站优惠政策,是减少出行成本的重要措施。(记者 王璐)'}
可以使用replace函数清洗掉转义字符。
新闻链接
很简单明了。上代码:
这里只给xpath匹配的代码
news_info = {}
news_info['title'] = html_tree.xpath('//h1[@class="a-title"]/strong/text()') # //选取所有元素 /选取子元素或属性
news_info['content'] = ''.join(html_tree.xpath('//div[@class="a-con"]/p/text()'))
打印输出:
{'title': ['美航管局新任局长就职 称737 MAX复飞没有时间表'],
'content': '中新网8月13日电 据外媒报道,美国联邦航空管理局(FAA)新任局长斯蒂芬•迪克森12日宣誓就任,他表示,FAA没有让737 MAX飞机复飞的时间表。资料图:波音737 MAX 8。7月24日,这位达美航空公司负责航班运营的前高级副总裁在参议院,以52票对40票的投票结果通过担任FAA局长的任命。'}
OK!我们完成了爬虫的准备工作,接下来,我们就要将爬虫实现了。我会选用scrapy框架实现爬虫的爬取与存储,存储我会选用elasticsearch。