校外也要读研 发表于 2021-6-18 21:23:43

为什么编写爬虫,idle什么也不运行,也不报错呢?

如题,代码完全照视频或作业答案,同时也照书上代码打了,都无法运行。。。

wp231957 发表于 2021-6-18 21:25:17

为啥不放代码?谁知道你在说啥?

Twilight6 发表于 2021-6-18 22:35:53



问问题养成贴代码的习惯,否则和纸上谈兵没什么区别

校外也要读研 发表于 2021-6-18 22:36:30

file:///C:/Users/Administrator/Desktop/%E6%96%B0%E5%BB%BA%E4%BD%8D%E5%9B%BE%E5%9B%BE%E5%83%8F.bmp
import urllib.request
import re
from bs4 import BeautifulSoup

def main():
    url = "http://baike.baidu.com/view/284853.htm"
    response = urllib.request.urlopen(url)
    html = response.read()
    soup = BeautifulSoup(html, "html.parser")
   
    for each in soup.find_all(href=re.compile("view")):
      print(each.text, "->", ''.join(["http://baike.baidu.com", each["href"]]))

if __name__ == "__main__":
    main()

xiaosi4081 发表于 2021-6-18 22:51:41

校外也要读研 发表于 2021-6-18 22:36
import urllib.request
import re
from bs4 import BeautifulSoup


你这想找的是啥?

suchocolate 发表于 2021-6-18 23:49:40

from urllib import request
import re
from bs4 import BeautifulSoup


def main():
    url = "http://baike.baidu.com/view/284853.htm"
    headers = {'User-Agent': 'Firefox'}   # 得改header防反扒
    req = request.Request(url, headers=headers)
    r = request.urlopen(req)
    html = r.read().decode('utf-8')
    soup = BeautifulSoup(html, "html.parser")
    for each in soup.find_all(href=re.compile("view")):
      print(each.text, "->", ''.join(["http://baike.baidu.com", each["href"]]))


if __name__ == "__main__":
    main()
页: [1]
查看完整版本: 为什么编写爬虫,idle什么也不运行,也不报错呢?