本人萌新,大佬们帮忙看看代码哪里有错误,谢谢了
import requestsfrom bs4 import BeautifulSoup
url = "https://www.umeitu.com/bizhitupian/weimeibizhi/"
resp = requests.get(url)
resp.encoding = "utf-8"
main_page = BeautifulSoup(resp.text, "html.parser")
alist = main_page.find("div",class = "TypeList").find_all("a")
print(alist)
#for a in alist:
print(a.get('href'))
提示alist = main_page.find("div",class = "TypeList").find_all("a")
^
SyntaxError: invalid syntax 本帖最后由 xrryz 于 2022-3-20 18:32 编辑
alist = main_page.find(name="div", attrs={"class" :"TypeList"})
...
main_page = BeautifulSoup(resp.text, "html.parser")
lis=main_page.select(".TypeList > ul:nth-child(1)")
for li in lis:
try:
u=li.select('a')['href']
print(u)
except:
pass
试试class_ = class要写成class_ 语法错误 class是python的关键字,使用 class 做参数会导致语法错误.从Beautiful Soup的4.1.1版本开始,可以通过 class_ 参数来搜索。 class语法错误,改一下吧
import requests
from bs4 import BeautifulSoup
url = "https://www.umeitu.com/bizhitupian/weimeibizhi/"
resp = requests.get(url)
resp.encoding = "utf-8"
main_page = BeautifulSoup(resp.text, "html.parser")
alist = main_page.find("div",class_="TypeList").find_all("a")
print(alist)
for a in alist:
print(a.get('href')) 谢谢大家,class_就可以了,谢谢谢谢
页:
[1]