前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用python访问网页

使用python访问网页

作者头像
py3study
发布2020-01-08 11:10:46
4K0
发布2020-01-08 11:10:46
举报
文章被收录于专栏:python3python3

python版本:3

访问页面:

代码语言:javascript
复制
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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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结合实际修改

代码语言:javascript
复制
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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档