如何用python解析google搜索结果?

2024-04-18 08:57:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我的单子一直空着

import requests
import re
from bs4 import BeautifulSoup

keywords = ['"site:instagram.com" "@gmail.com" "gadgets"']

url = ('https://google.com/search?q='+''.join(keywords))

response = requests.get(url)

soup = BeautifulSoup(response.text, "html.parser")

emails = soup.body.findAll(r"[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+]" ,re.I)

print(emails)

没有显示错误消息


Tags: fromimportrecomurlresponserequests单子
1条回答
网友
1楼 · 发布于 2024-04-18 08:57:53

美人鱼似乎有点过分了。您可以通过简单的re.findall来实现这一点

import requests
import re

# search terms in a list
keywords = ["site:instagram.com", "@gmail.com", "gadgets"]

# join the list using the plus character
url = 'https://google.com/search?q={}'.format('+'.join(keywords))
print(url)

response = requests.get(url)

# \w means word
regex = r"[\w._-]+@[\w._-]+\.[\w._-]+"

emails = re.findall(regex, str(response.content))
print(emails)

相关问题 更多 >