我一直在尝试从成功提取的URL列表中读取链接。我的问题是,当我尝试阅读整个列表时得到一个TypeError Traceback (most recent call last)
。但是,当我阅读单个链接时,urlopen(urls).read()
行会毫无问题地执行。
response = requests.get('some_website')
doc = BeautifulSoup(response.text, 'html.parser')
headlines = doc.find_all('h3')
links = doc.find_all('a', { 'rel':'bookmark' })
for link in links:
print(link['href'])
for urls in links:
raw_html = urlopen(urls).read() <----- this row here
articles = BeautifulSoup(raw_html, "html.parser")
参考方案
考虑将BeautifulSoup
与requests.Session()
一起使用,以提高重用连接和添加标头的效率
import requests
from bs4 import BeautifulSoup
with requests.Session() as s:
url = 'https://newspunch.com/category/news/us/'
headers = {'User-Agent': 'Mozilla/5'}
r = s.get(url, headers = headers)
soup = BeautifulSoup(r.text, 'lxml')
links = [item['href'] for item in soup.select('[rel=bookmark]')]
for link in links:
r = s.get(link)
soup = BeautifulSoup(r.text, 'lxml')
print(soup.select_one('.entry-title').text)
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
Python:如何根据另一列元素明智地查找一列中的空单元格计数? - pythondf = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…
Python pytz时区函数返回的时区为9分钟 - python由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…
将字符串分配给numpy.zeros数组[重复] - pythonThis question already has answers here: Weird behaviour initializing a numpy array of string data (4个答案) …
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…