Joh*_*Doe 4 python beautifulsoup web-scraping
按照 这个关于 使用 Python 和 BeautifulSoup 进行网页抓取的教程来学习诀窍 - 但是 Pycharm 返回一个我不明白的错误
你好呀!
尝试了上面提到的教程 ,使用调整后的链接作为实际链接,教程已过期(我使用的新链接)但是,当我单击“运行”时,我收到几个错误尝试了 PyCharm 的类型提示但无济于事。
import requests
from bs4 import BeautifulSoup
r = requests.get('https://pyvideo.org/events/pycon-se-2018.html')
soup = BeautifulSoup(r.text, 'html.parser')
results = [a.attrs.get('href') for a in soup.select('h4.entry-title a[href^=/pycon]')]
print(results)
Run Code Online (Sandbox Code Playgroud)
预期是链接列表。我得到的是一堆错误
Traceback (most recent call last):
File "/Users/maxschmitt/PycharmProjects/tester2/tester.py", line 7, in <module>
results = [a.attrs.get('href') for a in soup.select('h4.entry-title a[href^=/pycon]')]
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/element.py", line 1376, in select
return soupsieve.select(selector, self, namespaces, limit, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/soupsieve/__init__.py", line 114, in select
return compile(select, namespaces, flags, **kwargs).select(tag, limit)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/soupsieve/__init__.py", line 63, in compile
return cp._cached_css_compile(pattern, namespaces, custom, flags)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/soupsieve/css_parser.py", line 209, in _cached_css_compile
CSSParser(pattern, custom=custom_selectors, flags=flags).process_selectors(),
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/soupsieve/css_parser.py", line 1048, in process_selectors
return self.parse_selectors(self.selector_iter(self.pattern), index, flags)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/soupsieve/css_parser.py", line 882, in parse_selectors
key, m = next(iselector)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/soupsieve/css_parser.py", line 1035, in selector_iter
raise SelectorSyntaxError(msg, self.pattern, index)
soupsieve.util.SelectorSyntaxError: Malformed attribute selector at position 16
line 1:
h4.entry-title a[href^=/pycon]
Run Code Online (Sandbox Code Playgroud)
你知道我做错了什么吗?任何帮助,将不胜感激!
非常感谢!
您需要用/pycon“”换行或用\转义它
import requests
from bs4 import BeautifulSoup
r = requests.get('https://pyvideo.org/events/pycon-se-2018.html')
soup = BeautifulSoup(r.text, 'html.parser')
results = [a.attrs.get('href') for a in soup.select('h4.entry-title a[href^="/pycon"]')]
print(results)
Run Code Online (Sandbox Code Playgroud)
或者
results = [a.attrs.get('href') for a in soup.select('h4.entry-title a[href^=\/pycon]')]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3580 次 |
| 最近记录: |