Python使用Xpath轻松爬虫(脑残式)

1.在PyCharm安装lxml.

2.找到源码

3.F12、copy源码的xpath

Python使用Xpath轻松爬虫(脑残式)_第1张图片

4.代码

from lxml import etree
import requests

wb_data = requests.get("https://www.baidu.com/").text
html = etree.HTML(wb_data)
html_data = html.xpath('//*[@id="lh"]/a[2]');
for i in html_data:
    print(i.text)

  

转载于:https://www.cnblogs.com/ZaraNet/p/9938347.html

你可能感兴趣的:(Python使用Xpath轻松爬虫(脑残式))