Python 官方文档:入门教程 => 点击学习
python版本:3 访问页面: import urllib.request url="https://blog.csdn.net/qq_33160790" req=urllib.request.Request(url) r
python版本:3
访问页面:
import urllib.request
url="https://blog.csdn.net/qq_33160790"
req=urllib.request.Request(url)
resp=urllib.request.urlopen(req)
data=resp.read().decode('utf-8')
print(data)
效果:
抓取csdn页面中文章的链接:
xpath语法可以看这篇文章:
Http://www.w3school.com.cn/xpath/xpath_syntax.asp
from lxml import etree
import requests
url='https://blog.csdn.net/qq_33160790'
resp=requests.get(url)
if resp.status_code==requests.codes.ok:
html=etree.HTML(resp.text)
hrefs=html.xpath('////span[@class="link_title"]/a/@href')
for href in hrefs:
print href
效果:
打印出所有文章url:
from lxml import etree
import requests
for i in range(1,23): #23 is equal to pagelist-1
#print(i)
url='https://blog.csdn.net/qq_33160790/article/list/'+str(i)
resp=requests.get(url)
if resp.status_code==requests.codes.ok:
html=etree.HTML(resp.text)
hrefs=html.xpath('////span[@class="link_title"]/a/@href')
for href in hrefs:
print href
刷csdn点击脚本:
PS:url和23结合实际修改
from lxml import etree
import requests
import urllib.request
for i in range(1,23): #23 is equal to pagelist-1
#print(i)
url='https://blog.csdn.net/qq_33160790/article/list/'+str(i)
resp=requests.get(url)
if resp.status_code==requests.codes.ok:
html=etree.HTML(resp.text)
hrefs=html.xpath('////span[@class="link_title"]/a/@href')
for href in hrefs:
print (href)
req=urllib.request.Request(href)
data=urllib.request.urlopen(req).read()
--结束END--
本文标题: 使用python访问网页
本文链接: https://lsjlt.com/news/188409.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0