urllib库与requests库爬虫

首先介绍urllib库爬取网页内容。
需要lxml,urllib库
以我的博客为例爬取相关资料

import urllib.parse
import lxml.html
import urllib.request
import time
url='https://blog.csdn.net/Xiang_lhh/article/details/104940609'
#
resp=urllib.request.urlopen(url)#提交请求
html=lxml.html.parse(resp)
ps=html.xpath('//p/text()')#爬取p标签的内容,涉及到定位元素
for p in ps :
	print(p)
time.sleep(5)

此时将会把p标签中的内容输出。
requests库

import requests
import time
import lxml.html
from lxml import etree
test_url='https://blog.csdn.net/Xiang_lhh/article/details/104940609'
resp=requests.get(url=test_url).text
html=etree.HTML(resp)
ps=html.xpath('//p/text()')
for p in ps :
	print(p)
time.sleep(5)

此时,使用requests库,将内容输出

猜你喜欢

转载自blog.csdn.net/Xiang_lhh/article/details/105332865