这是可能的。 List of mobile user-agents.
但不是迭代 requests.get() 结果,您需要将其传递给 BeautifulSoup 对象,然后选择某个元素(带有数据的容器)并对其进行迭代。
# container with mobile layout data
for mobile_result in soup.select('.xNRXGe'):
title = mobile_result.select_one('.q8U8x').text
link = mobile_result.select_one('a.C8nzq')['href']
snippet = mobile_result.select_one('.VwiC3b').text
代码和example in the online IDE:
from bs4 import BeautifulSoup
import requests, lxml
headers = {
'User-agent':
'Mozilla/5.0 (Linux; Android 7.0; LGMP260) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.93 Mobile Safari/537.36'
}
params = {
'q': 'how to create minecraft server',
'gl': 'us', # country to search from
'hl': 'en', # language
}
html = requests.get('https://www.google.com/search', headers=headers, params=params)
soup = BeautifulSoup(html.text, 'lxml')
for mobile_result in soup.select('.xNRXGe'):
title = mobile_result.select_one('.q8U8x').text
link = mobile_result.select_one('a.C8nzq')['href']
snippet = mobile_result.select_one('.VwiC3b').text
print(title, link, snippet, sep='\n')
-----------
'''
How to Setup a Minecraft: Java Edition Server – Home
https://help.minecraft.net/hc/en-us/articles/360058525452-How-to-Setup-a-Minecraft-Java-Edition-Server
How to Setup a Minecraft: Java Edition Server · Go to this website and download the minecraft_server. · After you have downloaded it, make a folder on your ...
Minecraft Server Download
https://www.minecraft.net/en-us/download/server
Download the Minecraft: Java Edition server. Want to set up a multiplayer server? · Download minecraft_server.1.17.1.jar and run it with the following command:.
# other results
'''
或者,您可以使用来自 SerpApi 的 Google Organic Results API 来实现相同的目的。这是一个带有免费计划的付费 API。查看playground。
您的情况的不同之处在于您不必弄清楚事情(嗯,您必须,但这是一个更简单的过程),不必维护解析器随着时间的推移,并迭代结构化 JSON。
要集成的代码:
import os
from serpapi import GoogleSearch
params = {
"api_key": os.getenv("API_KEY"),
"engine": "google",
"q": "how to create minecraft server",
"hl": "en",
"gl": "us",
"device": "mobile" # mobile results
}
search = GoogleSearch(params)
results = search.get_dict()
for result in results["organic_results"]:
print(result['title'])
print(result['link'])
-----------
'''
How to Setup a Minecraft: Java Edition Server – Home
https://help.minecraft.net/hc/en-us/articles/360058525452-How-to-Setup-a-Minecraft-Java-Edition-Server
How to Setup a Minecraft: Java Edition Server · Go to this website and download the minecraft_server. · After you have downloaded it, make a folder on your ...
Minecraft Server Download
https://www.minecraft.net/en-us/download/server
Download the Minecraft: Java Edition server. Want to set up a multiplayer server? · Download minecraft_server.1.17.1.jar and run it with the following command:.
# other results
'''
免责声明,我为 SerpApi 工作。