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

问题描述 投票:1回答:1

跟随this tutorial关于使用Python和BeautifulSoup进行Web Scraping来学习绳索 - 但是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]

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

非常感谢你!

python web-scraping beautifulsoup
1个回答
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]')]
© www.soinside.com 2019 - 2024. All rights reserved.