我一直收到错误消息"missing 1 required positional:'section_url'“
每次我尝试使用findall时,我都会得到这个错误。
刚开始学习python,如果有任何帮助,我们将不胜感激!
from bs4 import BeautifulSoup
import urllib3
def extract_data():
BASE_URL = "http://www.chicagotribune.com/dining/ct-chicago-rooftops-patios-eat-drink-outdoors-near-me-story.html"
http = urllib3.PoolManager()
r = http.request('GET', 'http://www.chicagotribune.com/dining/ct-chicago-rooftops-patios-eat-drink-outdoors-near-me-story.html')
soup = BeautifulSoup(r.data, 'html.parser')
heading = soup.find("div", "strong")
category_links = [BASE_URL + p.a['href'] for p in heading.findAll('p')]
return category_links
print(soup)
extract_data()发布于 2017-07-26 12:13:39
基于Accepted的答案,我认为这就是你想要的
from bs4 import BeautifulSoup
import urllib3
def extract_data():
BASE_URL = "http://www.chicagotribune.com/dining/ct-chicago-rooftops-patios-eat-drink-outdoors-near-me-story.html"
http = urllib3.PoolManager()
r = http.request('GET', 'http://www.chicagotribune.com/dining/ct-chicago-rooftops-patios-eat-drink-outdoors-near-me-story.html')
soup = BeautifulSoup(r.data, 'html.parser')
heading = soup.select('div strong')
print(heading)
category_links = [BASE_URL + p.a['href'] for p in [i for i, x in enumerate(heading) if x == "p"]]
return category_links
print(extract_data())发布于 2017-07-26 11:52:19
通常,NoneType object has no attribute类的错误意味着上游函数返回None,然后您没有检查它,而是试图访问它的方法:
stuff = get_stuff() # this returns None
stuff.do_stuff() # this crashes最有可能的是,库找不到带有soup.find的标题。请尝试使用soup.select('div.strong')。
关于选择器的更多信息:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors
有关NoneType的更多信息:https://docs.python.org/3.6/library/constants.html#None
https://stackoverflow.com/questions/45317069
复制相似问题