抓了一段html格式的文件如這個:&http://www.xxxcom/.htm">u6aeu623du798fu3&,如果抓去裡面的href,要怎麼抓?抓到以後還要再一次抓的話,是要兩個for循環嗎?這不是html格式的嗎?這是怎麼回事呢?


也可以用pyquery或者bs4獲取啊。

doc.find("a").attr("href")

soup.find("a").get("href")

如果很多個href是有規律的,當然可以用for了。bs4是for item in find_all("a"):

print(item.get("href"))

import requests
from bs4 import BeautifulSoup

def main():

req = requests.get("http://www.dbmeinv.com/dbgroup/show.htm?cid=6")
print(req.encoding)
soup = BeautifulSoup(req.text)
imgLinks = soup.find_all("div", class_=img_single)
for link in imgLinks:
print(link.find("a").get("href"))

if __name__ =="__main__":
main()


有好幾種方法可以拿到鏈接:1,拿到渲染後的源代碼後,用查找字元串或者正則表達式清洗出想要的鏈接。2,xpath獲取節點。


一層層深入抓取,當前抓到這個片段含有href,那麼拿href裡面的網址再深入一層去抓。不過你給的這個例子的網址是不正確的
其實就是帶標識符的字元串
你需要一碗湯
謝邀!你想要的大概就是Beautiful Soup: We called him Tortoise because he taught us.


推薦閱讀:
相關文章