【问题标题】:'str' object has no attribute 'find_all'“str”对象没有属性“find_all”
【发布时间】:2019-06-20 09:21:27
【问题描述】:

我收到属性错误:“str”对象没有属性“find_all”

我关注了下面的帖子,但没有帮助。仅当包含行 print(a['title']) 时,我才会收到错误消息。 我试过 encode("utf-8") 它没有解决。

UnicodeEncodeError: 'charmap' codec can't encode characters

代码如下。它今天开始工作,没有任何改变!我在find_all下面确实有一个重复的代码,以前也有,我不知道哪个有效。

import requests # pip install requests
import bs4 # pip install BeautifulSoup4
from bs4 import BeautifulSoup

import pandas as pd # pip install pandas
import time
import io

def sc_data():

    URL = "www.website.com"
    #soup = BeautifulSoup(page.text, "html.parser").encode("utf-8")
    soup = BeautifulSoup(page.text, "html.parser")
    jobs = []
        for div in soup.find_all('div', attrs={'class':'row'}):
            for a in div.find_all('a', attrs={'data-tn-element':'jobTitle'}):
                print(a['title'])

jobs = []
    for div in soup.find_all(name='div', attrs={'class':'row'}):
        for a in div.find_all(name='a', attrs={'data-tn-element':'jobTitle'}):
            jobs.append(a["title"])
            return(jobs)
    print(jobs)

def main():
    sc_data()

main()

我正在做基本的网络抓取。它卡在无法读取编解码器 char'u\2013 和上述错误之间。

【问题讨论】:

  • 标题中的错误可能是由于尝试将字符串视为 BeautifulSoup 对象 - 您可能会更幸运地使您的第一个查询更具体,而不是迭代两次。此外,如果可能,请考虑使用 Python 3,因为它具有更好的 unicode 支持!

标签: python python-2.7 web-scraping


【解决方案1】:

您的问题缺乏一些细节,记录不充分。

好像你正在使用windows机器进行开发。

您可以按照我的以下建议可能会解决您的问题或记录有关您的代码的更多详细信息。

  1. 第 1 步:

    • 从远程服务器获取时编码为 utf-8。
  2. 第 2 步:

    • 加载时解码为 utf-8。

【讨论】:

    猜你喜欢
    • 2019-04-06
    • 2018-08-06
    • 1970-01-01
    • 2016-12-19
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    相关资源
    最近更新 更多