浏览器抓取网页(选择谷歌浏览器,避免了单一的标签无法定位到我们所需要的内容元素(图) )
优采云 发布时间: 2021-09-29 19:27浏览器抓取网页(选择谷歌浏览器,避免了单一的标签无法定位到我们所需要的内容元素(图)
)
它只是一个收录段落内容的标签。
#coding=utf-8
from bs4 import BeautifulSoup
import requests
#使用requests抓取页面内容,并将响应赋值给page变量
html = requests.get('https://www.qiushibaike.com/text/')
#使用content属性获取页面的源页面
#使用BeautifulSoap解析,吧内容传递到BeautifulSoap类
soup = BeautifulSoup(html.content,'lxml')
links = soup.find_all('div',class_='content')
#link的内容就是div,我们取它的span内容就是我们需要段子的内容
for link in links:
print link.span.get_text()
操作结果:
2. select() 方法
我们需要的内容可以逐层查找。这个特别方便,也就是定位,防止单个标签无法定位到我们需要的内容元素。
soup.select("html head title") #标签层级查找
soup.select("td div a") #标签路径 td-->div-->a
soup.select('td > div > a') #note:推荐使用这种记法
选择谷歌浏览器,右键复制--复制选择器
获取内容的任务:
#qiushi_tag_120529403> a> div> span:nth-child(1)
对于这个内容,我需要改一下~按标签顺序搜索
div> a> div> span(我在运行的时候发现了一个问题,>前后必须有空格,否则会报错)
然后代码如下:(和前面的代码区别只有最后三行)
#coding=utf-8
from bs4 import BeautifulSoup
import requests
#使用requests抓取页面内容,并将响应赋值给page变量
html = requests.get('https://www.qiushibaike.com/text/')
#使用content属性获取页面的源页面
#使用BeautifulSoap解析,吧内容传递到BeautifulSoap类
soup = BeautifulSoup(html.content,'lxml')
#我是分隔符,下面就是select()方法咯~
links = soup.select('div > a >div >span')
for link in links:
print link.get_text()
操作结果:
啊哦~有没有发现和上面操作的结果不一样?评论也在这里匹配,所以:
我们还需要修改定位使其更加精确,从标签a开始,并添加其class属性。a.contentHerf 写在 select 方法中。
具体是这样的:a.contentHerf>div>span(改成代码第三行再运行一次~)
注意:1)示例只介绍了一页的内容
2)代码在python中运行2.7
原来的:
================================================== ====================
【笔记】
提示:类型错误:“响应”类型的对象没有 len()
源代码:
url = 'https://www.imooc.com/course/list?c=python'
wb_data = requests.get(url)
Soup = BeautifulSoup(wb_data, 'lxml', from_encoding='utf-8')
报错的原因是因为这里的wb_data是一个requests对象,BeautifulSoup无法解析。您可以在 wb_data 后添加内容
汤 = BeautifulSoup(wb_data.content,'lxml', from_encoding='utf-8')
Tips:python3中request.urlopen()和requests.get()方法的区别
urlopen 打开URL URL,url参数可以是字符串url或Request对象,返回的对象是http.client.HTTPResponse。http.client.HTTPResponse 对象大概包括 read(), readinto(), getheader(), getheaders(), fileno(), msg, version, status, reason, debuglevel 和 closed 函数,其实一般来说就是decode使用read()函数后需要()函数
requests.get()方法请求站点的URL,然后打印出返回结果的类型、状态码、编码方式、Cookies等。返回一个Response对象,该对象存储了服务器返回的所有信息,包括响应头、响应状态码等。返回的网页部分将存储在.content和.text两个对象中。文本返回 Unicode 数据,内容返回二进制数据。换句话说,如果要获取文本,可以使用 r.text。如果你想拍照和文件,你可以使用 r.content
经验:请求网页的三种方式
# 网页下载器代码示例
2 import urllib
3
4 url = "http://www.baidu.com"
5
6 print("第一种方法: 直接访问url")
7 response1 = urllib.request.urlopen(url)
8 print(response1.getcode()) # 状态码
9 print(len(response1.read())) # read读取utf-8编码的字节流数据
10
11 print("第二种方法: 设置请求头,访问Url")
12 request = urllib.request.Request(url) # 请求地址
13 request.add_header("user-agent", "mozilla/5.0") # 修改请求头
14 response2 = urllib.request.urlopen(request)
15 print(response2.getcode())
16 print(len(response2.read()))
17
18 import http.cookiejar # 不知道这是啥
19
20 print("第三种方法: 设置coockie,返回的cookie")
21 # 第三种方法的目的是为了获取浏览器的cookie内容
22 cj = http.cookiejar.CookieJar()
23 opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
24 urllib.request.install_opener(opener)
25 response3 = urllib.request.urlopen(url)
26 print(response3.getcode())
27 print(len(response3.read()))
28 print(cj) # 查看cookie的内容
Tips:Python如何提取td中的内容
import re
m = re.findall(r'(.*?)', lines, re.I|re.M)
if m:
for x in m:
print x