Python网络爬虫快速上手
事先安装好,pycharm 打开File——>Settings——>Projext——>Project Interpriter

点击加号(图中红圈的地方)

点击红圈中的按钮

选中第一条,点击铅笔,将原来的链接替换为(这里已经替换过了): https://pypi.tuna.tsinghua.edu.cn/simple/ 点击OK后,输入requests-html然后回车 选中requests-html后点击Install Package

等待安装成功,关闭
实例内容: 从某博主的所有文章爬取想要的内容。 实例背景: 从(https://me.csdn.net/weixin_44286745)博主的所有文章获取各文章的标题,时间,阅读量。
1.导入requests_html中HTMLSession方法,并创建其对象
from requests_html import HTMLSession
session = HTMLSession()2.使用get请求获取要爬的网站,得到该网页的源代码。
html = session.get("https://me.csdn.net/weixin_44286745").htmlallBlog=html.xpath("//dl[@class='tab_page_list']") 

for i in allBlog:
title = i.xpath("dl/dt/h3/a")[0].text
views = i.xpath("//div[@class='tab_page_b_l fl']")[0].text
date = i.xpath("//div[@class='tab_page_b_r fr']")[0].text
print(title +' ' +views +' ' + date )
网页分析:

阅读量和时间也是重复的操作

可以用相对路径也可以用绝对路径,一般都是用相对路径,格式仿照代码。
第五行代码,每得到一篇文章的信息就输出,遍历完就可以获得全部的信息。
完整代码:
from requests_html import HTMLSession
session = HTMLSession()
html = session.get("https://me.csdn.net/weixin_44286745").html
allBlog=html.xpath("//dl[@class='tab_page_list']")
for i in allBlog:
title = i.xpath("dl/dt/h3/a")[0].text
views = i.xpath("//div[@class='tab_page_b_l fl']")[0].text
date = i.xpath("//div[@class='tab_page_b_r fr']")[0].text
print(title +' ' +views +' ' + date )
可以自己爬其他东西,如文章图片,动手试试吧!!!

- END -