首页 > 其他 > 详细

bilibili视频-爬虫

时间:2020-02-08 21:13:17      阅读:97      评论:0      收藏:0      [点我收藏+]

1

import requests
url = "https://item.jd.com/2967929.html"
try:
    r = requests.get(url)
    r.raise_for_status()   ##
    r.encoding = r.apparent_encoding
    print(r.text[:1000])
except:
    print("爬取出错")

ip地址查询

url = "http://m.ip138.com/ip.asp?ip="
try:
    r = requests.get(url + "202.204.80.112")
    r.raise_for_status()   ##
    r.encoding = r.apparent_encoding
    print(r.text[-500:])  ##防止文本过大导致卡机
except:
    print("爬取失败")

beautifulsoup

from bs4  import BeautifulSoup
import requests
r = requests.get("http://python123.io/ws/demo.html")
r.text
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
#soup = BeautifulSoup(open("d://demo.html"), "html.parser")

print(soup.prettify())  ##格式化打印

基本元素

标签使用

soup.a.name 
tag = soup.a
tag.attrs
tag.attrs['class']
tag.attrs['href']

技术分享图片
技术分享图片
技术分享图片

标签树上行遍历

for  parent in soup.a.parents:
    if parent is None:
        print(parent)
    else: 
        print(parent.name)

技术分享图片

技术分享图片
技术分享图片
技术分享图片

bilibili视频-爬虫

原文:https://www.cnblogs.com/g2thend/p/12285118.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!