|
10鱼币
- import urllib.request
- import urllib.parse
- import re
- from bs4 import BeautifulSoup
- def main():
- keyword = input("请输入关键词:")
- keyword = urllib.parse.urlencode({"word":keyword})
- response = urllib.request.urlopen("http://baike.baidu.com/search/word?%s" % keyword)
- html = response.read()
- soup = BeautifulSoup(html, "html.parser")
- for each in soup.find_all(href=re.compile("view")):
- content = ''.join([each.text])
- url2 = ''.join(["http://baike.baidu.com", each["href"]])
- response2 = urllib.request.urlopen(url2)
- html2 = response2.read()
- soup2 = BeautifulSoup(html2, "html.parser")
- if soup2.h2: ---------------------------------------------->请问前辈.h2是什么用法,难道第三层是.h3?,我在https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/index.html没有找到这种用法
- content = ''.join([content, soup2.h2.text])
- content = ''.join([content, " -> ", url2])
- print(content)
- if __name__ == "__main__":
- main()
复制代码
原帖地址:http://bbs.fishc.com/thread-72044-1-1.html
查看对象有什么属性或者方法 可以print(dir(obj))这样就知道可以点什么了
h2是标签 soup对象相当于python中的dom对象 可以点子标签就代表了那个标签对象
因为是面向对象的 就不用re匹配了
|
最佳答案
查看完整内容
查看对象有什么属性或者方法 可以print(dir(obj))这样就知道可以点什么了
h2是标签 soup对象相当于python中的dom对象 可以点子标签就代表了那个标签对象
因为是面向对象的 就不用re匹配了
|