这段代码显示的是美国的谷歌搜索结果,而不是英国的结果。我该如何更改这一点?我尝试更改域从'google.co.uk'而不是google.com,但这出乎意料地没有工作,因为英国的谷歌搜索引擎也使用google.com。
# Import the beautifulsoup and request libraries of python.
import requests
import bs4
# Make two strings with default google search URL
# 'https://google.co.uk/search?q=' and
# our customized search keyword.
# Concatenate them
texts = {'womens trousers', 'lingerie','pyjamas'}
#text= "women's trousers"
for text in texts:
url = 'https://www.google.co.uk/search?q=' + text
request_result=requests.get( url )
# Creating soup from the fetched request
soup = bs4.BeautifulSoup(request_result.text,
"html.parser")
# soup.find.all( h3 ) to grab
# all major headings of our search result,
heading_object=soup.find_all( 'h3' )
# Iterate through the object
# and print it as a string.
for info in heading_object:
print(info.getText())
1条答案
按热度按时间pu82cl6c1#
最初,它正在返回:
但是我在这里修复了代码,添加了
{text}&gl=gb&num=10
来指定great Britain,并为每个搜索项显示10个结果:现在,它为
texts
字典中的每个搜索词返回正确的10个搜索结果: