本段代码用于对key_word.txt中的搜索词进行百度搜索,获取百度内容页(首页)的推广广告数。
其中为了防止特例偏差,所以每个关键词爬取了10次,并取均值作为最终展示的广告数量。
主要运用的Beautifulsoup函数库,爬取代码在request函数中.
Beautifulsoup手册:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
import requests
from bs4 import BeautifulSoup
from itertools import repeat
# 发送HTTP请求时的HEAD信息,用于伪装为浏览器
headersParameters = {
'Connection': 'Keep-Alive',
'Accept': 'text/html, application/xhtml+xml, */*',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'User-Agent': 'Mozilla/6.1 (Windows NT 6.3; W