|
|
发表于 2020-8-6 15:56:18
|
显示全部楼层
本楼为最佳答案
本帖最后由 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("已完成")
复制代码 |
|