python搜索引擎根据关键词爬取内容

1.常见搜索引擎搜索格式:
(1)百度搜索引擎:
http://www.baidu.com.cn/s?wd=’ 关键词’&pn=‘分页’。
wd是搜索的关键词,pn是分页的页面,由于百度搜索每页的结果是十个(最上面的可能是广告推广,不是搜索结果),所以pn=0是第一页,第二页是pn=10…
例如https://www.baidu.com/s?wd=python&pn=0,得到的是关于python的第一页搜索结果。
(2)必应搜索引擎:
http://global.bing.com/search?q=‘关键词’
(3)搜狗搜索引擎
https://www.sogou.com/web?query=‘关键词’
(4)360搜索引擎
https://www.so.com/s?q=‘关键词’

解决eclipse Pydev中import时报错Unresolved import requests的方法
https://blog.csdn.net/chris_111x/article/details/52312523

3.安装lxml
在这里插入图片描述
出现这样的错误,说明安装的不是对应python版本的库,下载的库名中cp27代表python2.7,其它同理。(自己安装的是python3.8的版本)
下载对应的3.8版本,安装成功

4.百度搜索得到的链接被重定向了,需要禁止自动跳转,获取原本指向的地址,然后得到正确的域名

def get_real(o_url):
    #获取重定向url指向的网址 
    #try except对网页中的视频进行捕获异常
    try:
        r = requests.get(o_url, allow_redirects=False)  #禁止自动跳转
    except:
        return None
    if r.status_code == 302:
        try:
            return r.headers['location']  #返回指向的地址
        except:
            pass
    return o_url

5.使用Python 内置的模块 urlparse可以解析网址
result = urlparse(real[“sub_url”]).netloc

6.在使用该方法的k[‘href’]读取网页链接时,编译器报错:
KeyError: ‘href’
修改为:
k.get(‘href’)
成功运行,取出href中的链接。

7.获取的网址去重:
为了可以多重爬虫,把网址放在队列里
此时需要判重,对于每一个网址,可以用一个set集合(set不会存重复)来存放,一遇到新的网站先判断该网址是否已存在set中,如存在只不许加入队列,不存在则加入队列中

8.jieba分词:
下载地址: https://github.com/fxsjy/jieba
可以将其放到python安装目录下,方便管理
启动cmd, 输入 :python setup.py install

9.已当前时间命名

#记录当前时间            
	now = time.strftime("%Y%m%d%H%M%S",time.localtime(time.time())) 
#爬取的文件已当前时间命名并保存在data文件夹里
	filename="./文件名/"+now+r".txt"
# 写入文件
	file = open(filename, 'w+', encoding='utf-8')

10.安装pyMYSQL时加上镜像路径
pip3 install PyMySQL -i https://pypi.douban.com/simple

11.匹配拼接多个标签
def getWord(html):
bs = BeautifulSoup(html, “html.parser”) #实例化对象
listDiv = bs.findAll(“div”)
listP = bs.findAll(“p”)
listTd = bs.findAll(“td”)
listLi = bs.findAll(“li”)
listSpan = bs.findAll(“span”)
listDd = bs.findAll(“dd”)
listDt = bs.findAll(“dt”)
namelist = listDiv + listP + listTd + listLi + listSpan + listDd + listDt
return namelist

12.使用os.remove(path+dnsFile)出错
TypeError: can only concatenate str (not “_io.TextIOWrapper”) to str
路径不能用拼接的形式,应在之前进行拼接后传入单个变量
dataPath = path+dnsFile
os.remove(dataPath)

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爬取搜索引擎是一种获取搜索结果的方法,可以用于获取特定关键词搜索结果。在Python中,可以使用Scrapy库来进行网页爬取。通过Scrapy,你可以获取网页的标题、URL和HTML等信息,并使用SQLite3来管理爬取的数据源。\[1\] 常见的搜索引擎搜索格式如下: - 百度搜索引擎:http://www.baidu.com.cn/s?wd=关键词&pn=分页。其中,wd是搜索关键词,pn是分页的页面。例如,https://www.baidu.com/s?wd=python&pn=0 是关于python的第一页搜索结果。\[2\] - 必应搜索引擎:http://global.bing.com/search?q=关键词 - 搜狗搜索引擎:https://www.sogou.com/web?query=关键词 - 360搜索引擎:https://www.so.com/s?q=关键词\[2\] 在Python中,可以使用urllib模块来模拟浏览器的工作,完成对网页的请求和点击。通过urlopen函数可以打开一个链接,并将信息传递给reqs变量。然后,可以使用with语句打开一个文件,并将reqs的内容写入文件中。\[3\] 总结起来,使用Python爬取搜索引擎的步骤如下: 1. 使用Scrapy库进行网页爬取,获取网页的标题、URL和HTML等信息。 2. 使用SQLite3来管理爬取的数据源。 3. 根据不同搜索引擎搜索格式,构建相应的URL,包含关键词和分页信息。 4. 使用urllib模块模拟浏览器的工作,发送请求并获取网页内容。 5. 将获取的网页内容保存到文件中,以便后续处理和分析。 希望以上信息对你有所帮助! #### 引用[.reference_title] - *1* [如何用Python爬取搜索引擎的结果](https://blog.csdn.net/weixin_39941262/article/details/111424503)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Python获取搜索引擎结果](https://blog.csdn.net/qq_32614873/article/details/128984678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Python 爬虫基础学习-(爬取搜索引擎首页的小爬虫,web请求过程剖析)](https://blog.csdn.net/weixin_55435895/article/details/119610253)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值