python - BeautifulSoup - 无法访问网页

标签 python web-scraping beautifulsoup

我正在尝试访问网页并返回该页面上的所有超链接。我正在使用已回答的问题中的相同代码 here .

我希望访问此correct page ,但它仅返回此 incorrect page 的内容。

这是我正在运行的代码:

import httplib2
from bs4 import SoupStrainer, BeautifulSoup

http = httplib2.Http()
status, response = http.request('https://www.iparkit.com/Minneapolis')

for link in BeautifulSoup(response, 'html.parser', parseOnlyThese=SoupStrainer('a')):
    if link.has_attr('href'):
        print (link['href'])

结果:

/account
/monthlyAccount
/myproducts
/search
/
{{Market}}/search?expressSearch=true&timezone={{timezone}}
{{Market}}/search?expressSearch=false&timezone={{timezone}}
{{Market}}/events
monthly?timezone={{timezone}}
/login?next={{ getNextLocation(browserLocation) }}
/account
/monthlyAccount
/myproducts
find
parking-app
iparkit-express
https://interpark.custhelp.com
contact
/
/parking-app
find
https://interpark.custhelp.com
/monthly
/iparkit-express
/partners
/privacy
/terms
/contact
/events

我不介意返回上述结果,但它不会返回任何可以让我到达我想要的页面的链接。也许它受到保护?任何想法或建议,先谢谢您。

最佳答案

您尝试抓取的页面是完整的 JavaScript 生成的。

在这种情况下,http.request('https://www.iparkit.com/Minneapolis') 几乎不会给出任何结果。

相反,您必须执行真正的浏览器所做的事情 - 处理 JavaScript,然后尝试抓取已处理的内容。为此,您可以尝试 Selenium

为您page ,运行 JavaScript 后,您将获得 ~84 个 URL,而尝试在不运行 JavaScript 的情况下进行抓取,您将获得 ~7 个 URL。

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome('PATH_TO_CHROME_WEBDRIVER', chrome_options=chrome_options)
driver.get('https://www.iparkit.com/Minneapolis')

content = driver.page_source

然后,您可以在您的案例中使用 BeautifulSoup 从该内容中提取您想要的内容。

关于python - BeautifulSoup - 无法访问网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50218738/

相关文章:

Python3,beautifulsoup,在特定页面不返回任何内容

python - 使用 Python 和 CQL 时如何从 Cassandra 返回时间戳?

python - Flask + uWSGI + Celery - 如何将 Celery 作为守护进程启动

selenium - headless 浏览器和抓取 - 解决方案

python - HTML 文本提取

python - 在 Scrapy 中利用 Beautifulsoup

c++ - 如何在 Google App Engine Python 2.7 运行时模拟 GNU C 库 drem/remainder 函数?

python - 如果超过最大值,则获取开始和停止之间的数字并重新启动计数器

python - 列出网络抓取中的索引超出范围错误

使用 BeautifulSoup 进行 Python 抓取