使用Python和BeautifulSoup启动Web爬网-分步教程中的错误

时间:2019-04-06 19:59:52

标签: python web-scraping beautifulsoup

  

关注   this tutorial   关于使用Python和BeautifulSoup进行Web爬取以学习绳索的知识-   但是Pycharm返回了我不理解的错误

你好!

尝试了above mentioned tutorial 将调整后的链接作为实际链接,该教程已过期(New link I used) 但是,当我单击“运行”时,出现几个错误 尝试使用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)

期望的是链接列表。我得到的是很多错误


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]

您知道我做错了什么吗? 任何帮助将不胜感激!

非常感谢您!

1 个答案:

答案 0 :(得分:2)

您需要将/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)

results = [a.attrs.get('href') for a in soup.select('h4.entry-title a[href^=\/pycon]')]