【发布时间】:2021-02-02 21:15:08
【问题描述】:
我正在尝试提取类 ['address'] 中的所有 href 链接。每次我运行代码时,我只得到前 5 个,仅此而已,即使我知道应该有 9 个。
我已经阅读了下面的各种线程,无数次修改了我的代码,包括切换所有解析器(html.parser、html5lib、lxml、xml、lxml-xml),但似乎没有任何效果。知道是什么导致它在第 5 次迭代后停止吗?我对python还是很陌生,所以如果这是我忽略的菜鸟错误,我深表歉意。任何帮助都将不胜感激,即使是讽刺的答案:)
-
Beautiful Soup 4 find_all don't find links that Beautiful Soup 3 finds
-
Python 64 bit not storing as long of string as 32 bit python
我在下面的以下网页上使用了非常相似的代码,并且在抓取 href 时没有遇到任何问题: https://www.walgreens.com/storelistings/storesbystate.jsp?requestType=locator https://www.walgreens.com/storelistings/storesbycity.jsp?requestType=locator&state=AK
我的代码如下:
import requests
from bs4 import BeautifulSoup
local_rg = requests.get('https://www.walgreens.com/storelocator/find.jsp?requestType=locator&state=AK&city=ANCHORAGE&from=localSearch')
local_rg_content = local_rg.content
local_rg_content_src = BeautifulSoup(local_rg_content, 'lxml')
for link in local_rg_content_src.find_all('div'):
local_class = str(link.get('class'))
if str("['address']") in str(local_class):
local_a = link.find_all('a')
for a_link in local_a:
local_href = str(a_link.get('href'))
print(local_href)
我的结果(前 5 名):
- /locator/walgreens-1470+w+northern+lights+blvd-anchorage-ak-99503/id=15092
- /locator/walgreens-725+e+northern+lights+blvd-anchorage-ak-99503/id=13656
- /locator/walgreens-4353+lake+otis+parkway-anchorage-ak-99508/id=15653
- /locator/walgreens-7600+debarr+rd-anchorage-ak-99504/id=12679
- /locator/walgreens-2197+w+dimond+blvd-anchorage-ak-99515/id=12680
但应该是 9:
- /locator/walgreens-1470+w+northern+lights+blvd-anchorage-ak-99503/id=15092
- /locator/walgreens-725+e+northern+lights+blvd-anchorage-ak-99503/id=13656
- /locator/walgreens-4353+lake+otis+parkway-anchorage-ak-99508/id=15653
- /locator/walgreens-7600+debarr+rd-anchorage-ak-99504/id=12679
- /locator/walgreens-2197+w+dimond+blvd-anchorage-ak-99515/id=12680
- /locator/walgreens-2550+e+88th+ave-anchorage-ak-99507/id=15654
- /locator/walgreens-12405+brandon+st-anchorage-ak-99515/id=13449
- /locator/walgreens-12051+old+glenn+hwy-eagle+river-ak-99577/id=15362
- /locator/walgreens-1721+e+parks+hwy-wasilla-ak-99654/id=12681
【问题讨论】:
标签: parsing web-scraping beautifulsoup python-requests python-3.8