栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python爬虫网页爬取关键词(谷歌浏览器怎么搜索关键词)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python爬虫网页爬取关键词(谷歌浏览器怎么搜索关键词)

import urllib
import requests
from bs4 import BeautifulSoup
# desktop user-agent
USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:65.0) Gecko/20100101 Firefox/65.0"
# mobile user-agent
MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36"
headers = {"user-agent": USER_AGENT}

query = "XXX"   # 查询关键
query = query.replace(' ', '+')
URL = f"https://google.com/search?q={query}"  # 谷歌查询网址
resp = requests.get(URL, headers=headers)

# 获得查询结果
if resp.status_code == 200:
    soup = BeautifulSoup(resp.content, "html.parser")
    results = []   #网站内容
    page = []   #第一页的页面url
    url = []
    for g in soup.find_all('div', class_='r'):
        anchors = g.find_all('a')
        if anchors:
            link = anchors[0]['href']
            title = g.find('h3').text
            item = {
                "title": title,
                "link": link
            }
            results.append(item)
            url.append(link)
    for a in soup.find_all('tr'):
        for b in a.find_all('a'):
            link = b['href']
            page.append(link)
print(results)
print(page)

输出示例:

[{'title': '大麻的12 件有趣知識(Weed: 12 Interesting Facts You Should ...', 'link': 'https://tw.voicetube.com/videos/44637'},...]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773003.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号