BS4 和 urllib:收集一些链接并将其存储在数组中
BS4 and urllib: gather some links and store it in an array
我正在尝试从以下网站获取有关 USA/Texas 一些城镇生活成本指数的一些数据:http://www.city-data.com/city/Texas.html
方法:为了从目标页面中重复提取链接,我使用以下函数:
from bs4 import BeautifulSoup
import requests
import re
def getLinks(url):
r = requests.get("http://www.city-data.com/city/Texas.html")
soup = BeautifulSoup(r.content)
links = []
for link in soup.findAll('a', attrs={'href': re.compile("^http://")}):
links.append(link.get('href'))
##It will scrape all the a tags, and for each a tags, it will append the href attribute to the links list.
return links
print( getLinks("http://www.city-data.com/city/Texas.html") )
数据集: http://www.city-data.com/city/Texas.html 包含以下页面,其中包含有关城镇居民的信息:
Abilene, TX 120,958
Abram-Perezville 6,663
Addison, TX 15,457
Alamo Heights 7,806
Alamo, TX 19,224
Aldine 15,869
Alice, TX 19,395
Allen, TX 94,179
Alton North 6,182
注意:从子页面收集数据的目的是什么:因此我需要一个循环遍历子页面的解析器 - 例如如下所示:
http://www.city-data.com/city/Abilene-Texas.html
http://www.city-data.com/city/Abram-Perezville-Texas.html
http://www.city-data.com/city/Addison-Texas.html
http://www.city-data.com/city/Alamo-Heights-Texas.html
等等-但现在我回来了
ModuleNotFoundError: No module named 'BeautifulSoup'
PS: 在第一次尝试中我使用了 urllib2 - 但这是 python2 - 所以我将其更改为 urllib3
但我不确定这是否正确——以及我的 Anaconda 中是否有此模块 运行。这非常重要。 顺便说一句:下面的术语怎么样:urllib2.urlopen - 这似乎也过时了!?我也需要重写这个。你觉得怎么样!?期待收到你的回复!
目前我对 urllib.urlopen 这个词有点困惑!?
更新: 感谢 Andrej 和 Guilherme 的提示,我看到我在包中有以下设置:
所以我需要重新编码我导入的插件。非常感谢提示!
将代码更改为
from bs4 import BeautifulSoup
运行 终端上的 pip list 命令并确保安装了 Beuatifulsoup 库。
example
C:\Users\xxxx>pip list
Package Version
---------------------- ----------
beautifulsoup4 4.8.2
我正在尝试从以下网站获取有关 USA/Texas 一些城镇生活成本指数的一些数据:http://www.city-data.com/city/Texas.html
方法:为了从目标页面中重复提取链接,我使用以下函数:
from bs4 import BeautifulSoup
import requests
import re
def getLinks(url):
r = requests.get("http://www.city-data.com/city/Texas.html")
soup = BeautifulSoup(r.content)
links = []
for link in soup.findAll('a', attrs={'href': re.compile("^http://")}):
links.append(link.get('href'))
##It will scrape all the a tags, and for each a tags, it will append the href attribute to the links list.
return links
print( getLinks("http://www.city-data.com/city/Texas.html") )
数据集: http://www.city-data.com/city/Texas.html 包含以下页面,其中包含有关城镇居民的信息:
Abilene, TX 120,958
Abram-Perezville 6,663
Addison, TX 15,457
Alamo Heights 7,806
Alamo, TX 19,224
Aldine 15,869
Alice, TX 19,395
Allen, TX 94,179
Alton North 6,182
注意:从子页面收集数据的目的是什么:因此我需要一个循环遍历子页面的解析器 - 例如如下所示:
http://www.city-data.com/city/Abilene-Texas.html http://www.city-data.com/city/Abram-Perezville-Texas.html http://www.city-data.com/city/Addison-Texas.html http://www.city-data.com/city/Alamo-Heights-Texas.html
等等-但现在我回来了
ModuleNotFoundError: No module named 'BeautifulSoup'
PS: 在第一次尝试中我使用了 urllib2 - 但这是 python2 - 所以我将其更改为 urllib3 但我不确定这是否正确——以及我的 Anaconda 中是否有此模块 运行。这非常重要。 顺便说一句:下面的术语怎么样:urllib2.urlopen - 这似乎也过时了!?我也需要重写这个。你觉得怎么样!?期待收到你的回复! 目前我对 urllib.urlopen 这个词有点困惑!?
更新: 感谢 Andrej 和 Guilherme 的提示,我看到我在包中有以下设置:
所以我需要重新编码我导入的插件。非常感谢提示!
将代码更改为
from bs4 import BeautifulSoup
运行 终端上的 pip list 命令并确保安装了 Beuatifulsoup 库。
example
C:\Users\xxxx>pip list
Package Version
---------------------- ----------
beautifulsoup4 4.8.2