![](/img/trans.png)
[英]Python Selenium: How to wait until href attribute contains a specific string?
[英]Python If String Contains in href
這是我的python代碼。
r = requests.get("myurl")
data = r.text
soup = BeautifulSoup(data, "lxml")
texttmp = ""
for link in soup.find_all('a'):
image = link.get("href")
if ".jpg" in image:
print(image)
當我嘗試運行此代碼時,出現以下錯誤。 我怎樣才能解決這個問題?
TypeError Traceback (most recent call last)
<ipython-input-35-618698d3a2d7> in <module>()
11 for link in soup.find_all('a'):
12 image = link.get("href")
---> 13 if ".jpg" in image:
14 print(image)
15
TypeError: argument of type 'NoneType' is not iterable
除了表示指向其他資源的鏈接之外,html錨標記<a ...>
還可以充當文檔中某個位置的命名標記,即所謂的名稱標記<a name=whatever>
,從而可以將標記的位置使用URL http://example.com/#whatever
的片段的鏈接的目標
這可能是您遇到的,因為名稱標簽沒有href來指示它們指向的資源。
您需要檢查href是否返回None,如果不是,則跳過該返回的標簽。
祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.