requests打开网页,BeautifulSoup解析网页,得到目标网址的相对地址,urllib.parse重组url地址
2018-03-01 17:08
399 查看
requests官网:http://www.python-requests.org/en/master/user/quickstart/
BeautifulSoup官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/#attributes
python标准库urllib.parse:https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlsplit
# 取中关村网页的第一页
import requests
r = requests.get('http://detail.zol.com.cn/cell_phone_index/subcate57_0_list_1_0_1_1_0_1.html')
# 将上一步取得的.html给BeautifulSoup解析
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.text, 'lxml')
# 找到下一页的链接
next_path = soup.find(attrs={"class": "small-page-next"})["href"]
# 解析url链接中的各个部分
from urllib.parse import urlparse
o = urlparse(r.url)
# 组合parse到的协议/网络地址,以及目的页面的相对路径
from urllib.parse import ParseResult
t = ParseResult(o.scheme, o.netloc, next_path, "","","")
# 根据重组的url集合,生成url字符串
from urllib.parse import urlunparse
next_url = urlunparse(t)
从中关村在线查找手机型号,在本地建立数据库。
网站每页显示48个手机,共39页,需要设计一个翻页程序。
上述代码获取翻页的用到的目标url,还需要加循环,将全部页遍历获得所有的手机型号。
BeautifulSoup官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/#attributes
python标准库urllib.parse:https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlsplit
# 取中关村网页的第一页
import requests
r = requests.get('http://detail.zol.com.cn/cell_phone_index/subcate57_0_list_1_0_1_1_0_1.html')
# 将上一步取得的.html给BeautifulSoup解析
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.text, 'lxml')
# 找到下一页的链接
next_path = soup.find(attrs={"class": "small-page-next"})["href"]
# 解析url链接中的各个部分
from urllib.parse import urlparse
o = urlparse(r.url)
# 组合parse到的协议/网络地址,以及目的页面的相对路径
from urllib.parse import ParseResult
t = ParseResult(o.scheme, o.netloc, next_path, "","","")
# 根据重组的url集合,生成url字符串
from urllib.parse import urlunparse
next_url = urlunparse(t)
从中关村在线查找手机型号,在本地建立数据库。
网站每页显示48个手机,共39页,需要设计一个翻页程序。
上述代码获取翻页的用到的目标url,还需要加循环,将全部页遍历获得所有的手机型号。
相关文章推荐
- Python3.7 爬虫(二)使用 Urllib2 与 BeautifulSoup4 抓取解析网页
- 爬虫解析出来的网页中文部分都是乱码(运用的模块requests, BeautifulSoup)
- 【网络】无法解析服务器的DNS地址?;能登陆QQ,无法打开网页
- C#根据特定URL网址获取网页源码(完整html代码)后用正则式匹配得到目标串
- Win2003打开网页时总是提示添加网址到信任站点的设置方法
- VC FindWindow 得到IE8多选项卡,当前浏览网页网址!!
- BeautifulSoup与requests爬取网页中文转码问题
- QQ、迅雷等类视情况可以上,但是网页无法打开的问题解析
- 网友发来ifeng网址,打开后却是QQ空间,总提示QQ未登录?原来是一个阴险的诱骗网页
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- fileupload插件调用upload.parseRequest(request)解析得到空值问题
- 【爬虫】BeautifulSoup解析网页
- Android中打开系统相机连续拍多张图片,并得到图片地址
- 可以ping,NDS解析正确,却无法打开网页
- win7系统 QQ能正常登陆,网页不能正常访问但nslookup与ping都能正常解析网址问题解决
- iOS 网址、链接、网页地址、下载链接等正则验证
- 【python】使用Python中的urlparse、urllib抓取和解析网页
- 用JavaScript生成Android SDK的下载地址(2)——异步加载、解析xml,得到全部SDK的下载地址
- 设置html链接的目标地址在新窗口打开