通用爬虫 html 解析,爬虫:HTTP请求与HTML解析(爬取某乎网站)

1. 发送web请求

1.1  requests

用requests库的get()方法发送get请求,常常会添加请求头"user-agent",以及登录"cookie"等参数

1.1.1  user-agent

登录网站,将"user-agent"值复制到文本文件

1.1.2  cookie

登录网站,将"cookie"值复制到文本文件

1.1.3  测试代码

importrequestsfrom requests.exceptions importRequestException

headers={'cookie': '','user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}#替换为自己的cookie

defget_page(url):try:

html= requests.get(url, headers=headers, timeout=5)if html.status_code == 200:print('请求成功')returnhtml.textelse: #这个else语句不是必须的

returnNoneexceptRequestException:print('请求失败')if __name__ == '__main__':

input_url= 'https://www.zhihu.com/hot'get_page(input_url)

1.2  selenium

多数网站能通过window.navigator.webdriver的值识别selenium爬虫,因此selenium爬虫首先要防止网站识别selenium模拟浏览器。同样,selenium请求也常常需要添加请求头"user-agent",以及登录"cookie"等参数

1.2.1  移除Selenium中window.navigator.webdriver的值

在程序中添加如下代码(对应老版本谷歌)

from selenium.webdriver importChromefrom selenium.webdriver importChromeOptions

option=ChromeOptions()

option.add_experimental_option('excludeSwitches', ['enable-automation'])

driver= Chrome(options=option)

time.sleep(10)

1.2.2  user-agent

登录网站,将"user-agent"值复制到文本文件,执行如下代码将添加请求头

from selenium.webdriver importChromefrom selenium.webdriver importChromeOptions

option=ChromeOptions()

option.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"')

1.2.3  cookie

因为selenium要求cookie需要有"name","value"两个键以及对应的值的值,如果网站上面的cookie是字符串的形式,直接复制网站的cookie值将不符合selenium要求,可以用selenium中的get_cookies()方法获取登录"cookie"

from selenium.webdriver importChromefrom selenium.webdriver importChromeOptionsimporttimeimportjson

option=ChromeOptions()

option.add_experimental_option('excludeSwitches', ['enable-automation'])

option.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"')

driver= Chrome(options=option)

time.sleep(10)

driver.get('https://www.zhihu.com/signin?next=%2F')

time.sleep(30)

driver.get('https://www.zhihu.com/')

cookies=driver.get_cookies()

jsonCookies=json.dumps(cookies)

with open('cookies.txt', 'a') as f: #文件名和文件位置自己定义

f.write(jsonCookies)

f.write('\n')

1.2.4  测试代码示例

将上面获取到的cookie复制到下面程序中便可运行

from selenium.webdriver importChromefrom selenium.webdriver importChromeOptionsimporttime

option=ChromeOptions()

option.add_experimental_option('excludeSwitches', ['enable-automation'])

driver= Chrome(options=option)

time.sleep(10)

driver.get('https://www.zhihu.com')

time.sleep(10)

driver.delete_all_cookies()#清除刚才的cookie

time.sleep(2)

cookie= {} #替换为自己的cookie

driver.add_cookie(cookie)

driver.get('https://www.zhihu.com/')

time.sleep(5)for i in driver.find_elements_by_css_selector('div[itemprop="zhihu:question"] > a'):print(i.text)

2. HTML解析(元素定位)

要爬取到目标数据首先要定位数据所属元素,BeautifulSoup和selenium都很容易实现对HTML的元素遍历

2.1  BeautifulSoup元素定位

下面代码BeautifulSoup首先定位到属性为"HotItem-title"的"h2"标签,然后再通过.text()方法获取字符串值

importrequestsfrom requests.exceptions importRequestException

headers={'cookie': '','user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}#替换为自己的cookie

defget_page(url):try:

html= requests.get(url, headers=headers, timeout=5)if html.status_code == 200:print('请求成功')returnhtml.textelse: #这个else语句不是必须的

returnNoneexceptRequestException:print('请求失败')defparse_page(html):

html= BeautifulSoup(html, "html.parser")

titles= html.find_all("h2", {'class': 'HotItem-title'})[:10]for title intitles:print(title.text())if __name__ == '__main__':

input_url= 'https://www.zhihu.com/hot'parse_page(get_page(input_url))

2.2  selenium元素定位

selenium元素定位语法形式与requests不太相同,下面代码示例(1.2.4 测试代码示例)采用了一种层级定位方法:'div[itemprop="zhihu:question"] > a',笔者觉得这样定位比较放心。

selenium获取文本值得方法是.text,区别于requests的.text()

from selenium.webdriver importChromefrom selenium.webdriver importChromeOptionsimporttime

option=ChromeOptions()

option.add_experimental_option('excludeSwitches', ['enable-automation'])

driver= Chrome(options=option)

time.sleep(10)

driver.get('https://www.zhihu.com')

time.sleep(10)

driver.delete_all_cookies()#清除刚才的cookie

time.sleep(2)

cookie= {} #替换为自己的cookie

driver.add_cookie(cookie)

driver.get('https://www.zhihu.com/')

time.sleep(5)for i in driver.find_elements_by_css_selector('div[itemprop="zhihu:question"] > a'):print(i.text)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值