gpt4 book ai didi

python - 使用 BeautifulSoup 抓取 Google 时出现语法错误

转载 作者:行者123 更新时间:2023-12-01 00:25:37 24 4
gpt4 key购买 nike

我正在抓取谷歌搜索结果。但是,我在执行此操作时反复出现语法错误。代码如下:

import urllib.request
from bs4 import BeautifulSoup
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/70.0'

url = "https://www.google.com/search?hl=en&q=python+wikipedia"
headers={'User-Agent':user_agent,}

request=urllib.request.Request(url,None,headers) #The assembled request
response = urllib.request.urlopen(request)
data = response.read()

soup= BeautifulSoup(data, 'html.parser')
l = soup.find_all('h' , 'attrs' = {"class":'LC20lb'})
print(l)

我明白了:

SyntaxError: keyword can't be an expression

在行 l = soup.find_all('h' , 'attrs' = {"class":'LC20lb'}) 。有人可以告诉我我做错了什么吗?

最佳答案

属性周围不应有撇号:

l = soup.find_all('h' ,   attrs  = {"class":'LC20lb'})
# not: _ _
#l = soup.find_all('h' , 'attrs' = {"class":'LC20lb'})
# ^ ^

关于python - 使用 BeautifulSoup 抓取 Google 时出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58602245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com