用美丽的汤解析跨度

时间:2019-06-28 09:53:39

标签: python beautifulsoup python-requests

我正在尝试通过某个网站进行解析,试图找到位于div标签和class_中的“ span”。如果跨度等于字符串(例如“ line”),则该跨度属于特定类别,则它将返回网站的实际链接。

我收到的错误消息:

  

第28行,在       soup = BeautifulSoup(url_html,“ html.parser”)第245行,在 init 中       elif len(markup)<= 256和(TypeError:类型为'Response'的对象没有len()

import csv
from bs4 import BeautifulSoup
import requests

contents = []

def condition_check():
    for sp in soup.find("div",class_='-vDIg'):
        check = sp.span
        if check in ['Line','LINE ID',]:
            return link


filename = 'link_business_filter.csv'

with(open(filename,'rt')) as f:
    data = csv.reader(f)

    for row in data:
        links = row[0]
        contents.append(links)



for link in contents:
    url_html = requests.get(link)
    soup = BeautifulSoup(url_html,"html.parser")
    con_fltr = condition_check()
    print(con_fltr)

1 个答案:

答案 0 :(得分:0)

您要将Request对象传递给Beautiful Soup,您需要像这样传递html内容:

for link in contents:
    url_html = requests.get(link)
    soup = BeautifulSoup(url_html.content,"html.parser")
    con_fltr = condition_check()
    print(con_fltr)

url_html-> url_html.content