python - 使用 BeauitifulSoup 进行网页抓取错误 : [Errno 10061]

标签 python web-scraping beautifulsoup

尝试使这段代码正常工作:(使用 BeautifulSoup 的网络抓取示例)

import urllib2    
wiki = "https://en.wikipedia.org/wiki/List_of_state_and_union_territory_capitals_in_India"
page = urllib2.urlopen(wiki)
from bs4 import BeautifulSoup
soup = BeautifulSoup(page)

我收到此错误:-

URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>

我想这与一些防火墙/安全相关的问题有关,有人可以帮助解决应该做什么吗?

最佳答案

您可以尝试使用 requests 这样的方法:

import requests
from bs4 import BeautifulSoup 

wiki = "https://en.wikipedia.org/wiki/List_of_state_and_union_territory_capitals_in_India"
page = requests.get(wiki).content
soup = BeautifulSoup(page)
<小时/>

如果你想获取表格,你可以像这样使用 pandas:

import pandas as pd

wiki = "https://en.wikipedia.org/wiki/List_of_state_and_union_territory_capitals_in_India"
df = pd.read_html(wiki)[1]
df2 = df.copy()
df2.columns = df.iloc[0]
df2.drop(0, inplace=True)
df2.drop('No.', axis=1, inplace=True)
df2.head()

输出:

enter image description here

关于python - 使用 BeauitifulSoup 进行网页抓取错误 : [Errno 10061],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41377692/

相关文章:

python - 使用 Python 和 BeautifulSoup 抓取时模拟单击链接

python - Gunicorn - ValueError : '<path_to_sock.sock>' is not a socket

python - 需要从一个txt文件读入字典

python - 如何绕过机器人检测并使用 python 抓取网站

python - 单击 Scrapy 中的按钮

ruby-on-rails - 搜索和抓取黑客新闻 - Ruby

python - BeautifulSoup 解析树的深度优先遍历

Python BeautifulSoup 没有抓取多个页面

python - 在 Pandas 中找到两个系列之间的交集

python - 有什么方法可以从 flask 中带有 '\n' 字符的字符串中添加新行?