爬取网易新闻头条部分信息

代码如下
可以自己更改爬取的信息

import requests
from lxml import etree

url = 'https://news.163.com'
resp = requests.get(url)
if resp.status_code != 200:
    raise Exception("请求失败")
html_content = resp.text

pattern1 = '//div[@class="mod_top_news2" and @id="js_top_news"]/h2/a/text()'
pattern2 = '//div[@class="mod_top_news2" and @id="js_top_news"]/ul[@class="top_news_ul"]/li/a/text()'

tree = etree.HTML(html_content)
new1 = tree.xpath(pattern1)
new2 = tree.xpath(pattern2)

for i in new1: print(i)
for j in new2: print(j)
posted @ 2018-12-27 12:01  可不可与给我你的微笑  阅读(180)  评论(0编辑  收藏  举报