英文:
Scrape query returns USA google search results instead of UK search results
问题
这段代码显示的是美国谷歌搜索结果,而不是英国的。如何更改这一点?我尝试将域从'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())
英文:
this code is showing the results for USA google search results instead of the UK. How can I change this? I tried to change the domain from 'google.co.uk' instead of google.com but that expectedly didn't work because the UK google search engine also uses 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
得分: 2
以下是您要翻译的部分:
初始时返回的内容:
Pyjamas - Amazon.com
------
pyjamas - Nordstrom
------
Pajamas - Wikipedia
------
Silk, Cotton and Pajamas Sets for Women - ASOS
------
Women's pyjamas | OYSHO United States
------
Pyjamas - Etsy
------
Pyjamas | PJ Sets - Boux Avenue
------
Pyjamas for women - CALIDA
------
The best pyjamas for total bedtime comfort (and style) - British GQ
------
Pyjamas Definition & Meaning - Dictionary.com
------
Pants & Leggings - Womens pants | Chinos & cropped pants | ASOS
------
Women's Trousers - Amazon.com
------
Shop Womens Trousers - Macy's
------
Womens Trouser Pants - Target
------
Womens Trousers - Bloomingdale's
------
Trouser Pants for Women - Ann Taylor
------
Women's Pants | Leather, Cargo, Dress & Wide Leg | H&M US
------
Women's Trouser Pants & Leggings - Nordstrom
------
Women's Wide-Leg Pants - Nordstrom
------
Women's Trousers | Dress Pants for Women - JCPenney
------
Lingerie
------
Underwear
------
Women's Sexy Lingerie Sets: Corsets, Bustiers, Teddies | Adore Me
------
Women's Lingerie | Victoria's Secret
------
Sexy Women's Lingerie Collection - Yandy
------
Women's Lingerie & Underwear - Nasty Gal
------
Muse Intimates
------
Soma
------
Patricia's
------
Amazon.com: Women's Lingerie
------
Sensual Women's Lingerie in Silk, Lace & Tulle | Intimissimi
------
Lingerie for Women - Target
------
Women's Lingerie & Sleepwear - ASOS
------
Lingerie, Hosiery & Shapewear - Women - Nordstrom
------
Shop All Lingerie & Intimates - Fashion Nova
------
现在已修复的代码部分:
for text in texts:
url = f"https://www.google.co.uk/search?q={text}&gl=gb&num=10"
现在返回的正确结果:
Women's Trousers & Leggings - ASOS
------
Women's Trousers | M&S
------
Trousers For Women - Matalan
------
Women's Trousers & Leggings - Dorothy Perkins
------
Women's Trousers & Leggings | John Lewis & Partners
------
Ladies Trousers | Cigarette, Capri & Cargo Pants for Women - Next UK
------
Women's Trousers | boohoo UK
------
Trousers For Women | Knitted & Casual Trousers - New Look
------
Women's Trousers | Smart & Casual Trousers | Boden UK
------
Women's Trousers | Joggers, Leggings, Linen & Chinos | H&M GB
------
Womens Lingerie & Underwear | Boux Avenue UK
------
Lingerie | Women's Underwear - Marks & Spencer
------
Sexy Lingerie & Women's Underwear | boohoo UK
------
Lingerie & Nightwear for Women - ASOS
------
Womens Underwear & Lingerie Sets - Pour Moi
------
Women's Underwear & Lingerie | PrettyLittleThing
------
Women's Lingerie & Underwear - Nasty Gal
------
Lingerie | Womens Underwear & Sets - House of Fraser
------
Women's Lingerie | Women's Underwear - Next UK
------
Women's Lingerie & Underwear - Matalan
------
Women's Pyjamas | M&S
------
Womens Pyjamas | Printed & Pattern Pyjamas Sets - Next UK
------
Women's Pyjamas & PJ Sets - ASOS
------
Pyjamas | PJ Sets - Boux Avenue
------
Women's Pyjamas & Nightwear - Women's PJ Sets - Matalan
------
Pyjamas for Women - Peacocks
------
Womens Pyjama sets | Very.co.uk
------
Pyjamas for Women - George at ASDA
------
Women's Pyjamas | Women's Nightwear | John Lewis & Partners
------
Women''s Pyjamas | Pyjama Sets & PJs | boohoo UK
英文:
Initially it was returning:
Pyjamas - Amazon.com
------
pyjamas - Nordstrom
------
Pajamas - Wikipedia
------
Silk, Cotton and Pajamas Sets for Women - ASOS
------
Women's pyjamas | OYSHO United States
------
Pyjamas - Etsy
------
Pyjamas | PJ Sets - Boux Avenue
------
Pyjamas for women - CALIDA
------
The best pyjamas for total bedtime comfort (and style) - British GQ
------
Pyjamas Definition & Meaning - Dictionary.com
------
Pants & Leggings - Womens pants | Chinos & cropped pants | ASOS
------
Women's Trousers - Amazon.com
------
Shop Womens Trousers - Macy's
------
Womens Trouser Pants - Target
------
Womens Trousers - Bloomingdale's
------
Trouser Pants for Women - Ann Taylor
------
Women's Pants | Leather, Cargo, Dress & Wide Leg | H&M US
------
Women's Trouser Pants & Leggings - Nordstrom
------
Women's Wide-Leg Pants - Nordstrom
------
Women's Trousers | Dress Pants for Women - JCPenney
------
Lingerie
------
Underwear
------
Women's Sexy Lingerie Sets: Corsets, Bustiers, Teddies | Adore Me
------
Women's Lingerie | Victoria's Secret
------
Sexy Women's Lingerie Collection - Yandy
------
Women's Lingerie & Underwear - Nasty Gal
------
Muse Intimates
------
Soma
------
Patricia's
------
Amazon.com: Women's Lingerie
------
Sensual Women's Lingerie in Silk, Lace & Tulle | Intimissimi
------
Lingerie for Women - Target
------
Women's Lingerie & Sleepwear - ASOS
------
Lingerie, Hosiery & Shapewear - Women - Nordstrom
------
Shop All Lingerie & Intimates - Fashion Nova
------
But I fixed the code here, adding {text}&gl=gb&num=10
to specify great Britain and display 10 results for each term:
for text in texts:
url = f"https://www.google.co.uk/search?q={text}&gl=gb&num=10"
Now it returns the correct 10 search results for each search term in the texts
dictionary:
Women's Trousers & Leggings - ASOS
------
Women's Trousers | M&S
------
Trousers For Women - Matalan
------
Women's Trousers & Leggings - Dorothy Perkins
------
Women's Trousers & Leggings | John Lewis & Partners
------
Ladies Trousers | Cigarette, Capri & Cargo Pants for Women - Next UK
------
Women's Trousers | boohoo UK
------
Trousers For Women | Knitted & Casual Trousers - New Look
------
Women's Trousers | Smart & Casual Trousers | Boden UK
------
Women's Trousers | Joggers, Leggings, Linen & Chinos | H&M GB
------
Womens Lingerie & Underwear | Boux Avenue UK
------
Lingerie | Women's Underwear - Marks & Spencer
------
Sexy Lingerie & Women's Underwear | boohoo UK
------
Lingerie & Nightwear for Women - ASOS
------
Womens Underwear & Lingerie Sets - Pour Moi
------
Women's Underwear & Lingerie | PrettyLittleThing
------
Women's Lingerie & Underwear - Nasty Gal
------
Lingerie | Womens Underwear & Sets - House of Fraser
------
Women's Lingerie | Women's Underwear - Next UK
------
Women's Lingerie & Underwear - Matalan
------
Women's Pyjamas | M&S
------
Womens Pyjamas | Printed & Pattern Pyjamas Sets - Next UK
------
Women's Pyjamas & PJ Sets - ASOS
------
Pyjamas | PJ Sets - Boux Avenue
------
Women's Pyjamas & Nightwear - Women's PJ Sets - Matalan
------
Pyjamas for Women - Peacocks
------
Womens Pyjama sets | Very.co.uk
------
Pyjamas for Women - George at ASDA
------
Women's Pyjamas | Women's Nightwear | John Lewis & Partners
------
Women's Pyjamas | Pyjama Sets & PJs | boohoo UK
------
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论