Python 如何提取锚定标签,如下所示<;a类=";“无悬停装饰”>;?
所以 假设我正在谷歌搜索“白俄”,一旦我们这样做,我们就会收到一些模型卡,如下图所示 现在,如果您查看HTML的Inspector,我们将看到这些卡片的HREF位于一个锚标记内,如下图所示,(…表示额外的内容)Python 如何提取锚定标签,如下所示<;a类=";“无悬停装饰”>;?,python,html,css,web-scraping,beautifulsoup,Python,Html,Css,Web Scraping,Beautifulsoup,所以 假设我正在谷歌搜索“白俄”,一旦我们这样做,我们就会收到一些模型卡,如下图所示 现在,如果您查看HTML的Inspector,我们将看到这些卡片的HREF位于一个锚标记内,如下图所示,(…表示额外的内容) 所以,我感兴趣的是从这些锚标记中提取href,如果它们用于搜索的话 我的尝试 导入请求 从bs4导入BeautifulSoup req=请求。获取(“https://www.google.com/search?q=White+俄文“) soup=BeautifulSoup(req
所以,我感兴趣的是从这些锚标记中提取href,如果它们用于搜索的话
我的尝试
导入请求
从bs4导入BeautifulSoup
req=请求。获取(“https://www.google.com/search?q=White+俄文“)
soup=BeautifulSoup(req.text'html.parser')
soup.find_all(“a”,{“class”:“a-no-hover-detection”})#它不返回任何内容
我对网络-刮削有点陌生,所以非常感谢您的帮助
我的第二个问题是,在任何给定的随机搜索中,当我们没有这样的卡片时,如何检测我们有这样的模型卡片
谢谢。要从谷歌服务器获得正确的响应,请指定
User-Agent
HTTP头和hl=en
参数(以获得英文结果)。另外,类名是a-no-hover-decoration
,而不是a-no-hover-detection
:
导入请求
从bs4导入BeautifulSoup
标题={
“用户代理”:“Mozilla/5.0(X11;Ubuntu;Linux x86_64;rv:88.0)Gecko/20100101 Firefox/88.0”
}
params={“q”:“白俄罗斯”,“hl”:“en”}
req=requests.get(
"https://www.google.com/search,params=params,headers=headers
)
soup=BeautifulSoup(req.text,“html.parser”)
对于汤中的a.find_all(“a”,{“class”:“a-no-hover-decoration”}):
打印(a[“href”])
印刷品:
https://www.liquor.com/recipes/white-russian/
https://www.delish.com/cooking/recipe-ideas/a29091466/white-russian-cocktail-recipe/
https://www.bbcgoodfood.com/recipes/white-russian
要从谷歌服务器获得正确的响应,请指定用户代理
HTTP头和hl=en
参数(以获得英文结果)。另外,类名是a-no-hover-decoration
,而不是a-no-hover-detection
:
导入请求
从bs4导入BeautifulSoup
标题={
“用户代理”:“Mozilla/5.0(X11;Ubuntu;Linux x86_64;rv:88.0)Gecko/20100101 Firefox/88.0”
}
params={“q”:“白俄罗斯”,“hl”:“en”}
req=requests.get(
"https://www.google.com/search,params=params,headers=headers
)
soup=BeautifulSoup(req.text,“html.parser”)
对于汤中的a.find_all(“a”,{“class”:“a-no-hover-decoration”}):
打印(a[“href”])
印刷品:
https://www.liquor.com/recipes/white-russian/
https://www.delish.com/cooking/recipe-ideas/a29091466/white-russian-cocktail-recipe/
https://www.bbcgoodfood.com/recipes/white-russian
您还可以使用Chrome扩展直观地抓取CSS选择器
从bs4导入美化组
导入请求,lxml
标题={
“用户代理”:
“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582”
}
response=requests.get('https://www.google.com/search?q=white 俄语',标题=标题)。文本
汤=BeautifulSoup(响应“lxml”)
#选择()方法:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-按css类
选择('.cv2VAd.a-no-hover-decoration'):
link=result['href']
打印(链接)
输出:
https://www.liquor.com/recipes/white-russian/
https://www.delish.com/cooking/recipe-ideas/a29091466/white-russian-cocktail-recipe/
https://www.kahlua.com/en-us/drinks/white-russian/
或者,您可以使用。这是一个付费API,免费试用5000次搜索 要集成的代码:
从serpapi导入谷歌搜索
参数={
“api密钥”:“您的api密钥”,
“引擎”:“谷歌”,
“q”:“白俄”,
“谷歌域名”:“google.com”,
}
搜索=谷歌搜索(参数)
结果=search.get_dict()
对于结果中的结果['recipes_results']:
链接=结果['link']
打印(链接)
输出:
https://www.liquor.com/recipes/white-russian/
https://www.delish.com/cooking/recipe-ideas/a29091466/white-russian-cocktail-recipe/
https://www.kahlua.com/en-us/drinks/white-russian/
免责声明,我为SerpApi工作
您还可以使用Chrome扩展直观地获取CSS选择器
从bs4导入美化组
导入请求,lxml
标题={
“用户代理”:
“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582”
}
response=requests.get('https://www.google.com/search?q=white 俄语',标题=标题)。文本
汤=BeautifulSoup(响应“lxml”)
#选择()方法:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-按css类
选择('.cv2VAd.a-no-hover-decoration'):
link=result['href']
打印(链接)
输出:
https://www.liquor.com/recipes/white-russian/
https://www.delish.com/cooking/recipe-ideas/a29091466/white-russian-cocktail-recipe/
https://www.kahlua.com/en-us/drinks/white-russian/
或者,您可以使用。这是一个付费API,免费试用5000次搜索 要集成的代码:
从serpapi导入谷歌搜索
参数={
“api密钥”:“您的api密钥”,
“引擎”:“谷歌”,
“q”:“白俄”,
“谷歌域名”:“google.com”,
}
搜索=谷歌搜索(参数)
结果=search.get_dict()
对于结果中的结果['recipes_results']:
链接=结果['link']
打印(链接)
输出:
https://www.liquor.com/recipes/white-russian/
https://www.delish.com/cooking/recipe-ideas/a29091466/white-russian-cocktail-recipe/
https://www.kahlua.com/en-us/drinks/white-russian/
免责声明,我为SerpApi工作
非常感谢你的帮助!理解我的错误