下载了网页源代码如何在到所要东西,求助
sos,我想爬取这个网站的商品链接,用到BeautifulSoup的时候不知道如何去找到所需要的东西里,怎么办import requests
import bs4
import re
import time
import openpyxl
def open_url(url):
headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'}
res = requests.get(url, headers=headers,timeout=500)
return res
def main():
host = "https://www.ebay.com.au/sch/i.html?_from=R40&_nkw=Silicone+Band+%E2%80%93+Garmin+Forerunner&_sacat=0&_ipg=200&_fcid=15&_pgn=1"
res = open_url(host)
soup = BeautifulSoup(res.text,"html.parser")
for info in soup.select(".s-item__info clearfix")
name = book.select("a")
if __name__=="__main__":
main()
print("已完成")
用XPath它不香吗? qiuyouzhi 发表于 2020-8-6 15:12
用XPath它不香吗?
好像是也,我去学习一下,感谢 本帖最后由 Twilight6 于 2020-8-6 15:57 编辑
你导入错误了,第一个导入应该 from bs4 import BeautifulSoup 因为你代码中直接用 BeautifulSoup 了
第二个错误你 for 循环忘记加冒号
第三个错误 BeautifulSoup 语法错误
参考代码,已经可以成功爬出网址:
import requests
from bs4 import BeautifulSoup
import re
import time
import openpyxl
def open_url(url):
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'}
res = requests.get(url, headers=headers, timeout=500)
return res
def main():
host = "https://www.ebay.com.au/sch/i.html?_from=R40&_nkw=Silicone+Band+%E2%80%93+Garmin+Forerunner&_sacat=0&_ipg=200&_fcid=15&_pgn=1"
res = open_url(host)
soup = BeautifulSoup(res.text, "html.parser")
for info in soup.find_all('a',class_="s-item__link"):
url = info['href']
print(url)
if __name__ == "__main__":
main()
print("已完成") xiaolongren969 发表于 2020-8-6 15:32
好像是也,我去学习一下,感谢
感谢,感谢,我学习一下 xiaolongren969 发表于 2020-8-6 16:03
感谢,感谢,我学习一下
4L 代码你去运行看看,链接已经全爬出来了 Twilight6 发表于 2020-8-6 16:15
4L 代码你去运行看看,链接已经全爬出来了
其实这个细心点就能检查出来,用得着来鱼C吗{:10_282:} 陈尚涵 发表于 2020-8-6 16:20
其实这个细心点就能检查出来,用得着来鱼C吗
主要是find_all用得不熟,{:10_282:}
页:
[1]