Я получаю текстовую ошибку, в то время как код применим для компании, использующей python beautifulsoup - PullRequest
0 голосов
/ 31 декабря 2018

из bs4 импорт Запрос на импорт BeautifulSoup

r = запросы.get ('https://www.yelu.in/category/advertising') суп = BeautifulSoup (r.text,' lxml ')

для ссылок в супе.find_all ('a', класс _ = 'm_company_link'): href = links ['href']

headers = {'User-Agent': 'Googleboat'}
r = requests.get("https://www.yelu.in/"+href,headers = headers)
soup = BeautifulSoup(r.text,'lxml')
company = {
    "company_name" : soup.select_one('#company_name').text,
    "address" : soup.select_one('div.text.location').text,
    "phone" : soup.select_one('div.text.phone').text,
    "mobile_phone" : soup.find('div',string = "Mobile 
     phone").find_next_sibling('div').text,
    "fax": soup.find('div',string = "Fax").find_next_sibling('div').text,
    "website" : soup.find('div',string = 
    "Website").find_next_sibling('div').text,
    "year" :soup.find('span',string = "Establishment year").next_sibling,
    "employees" :soup.find('span',string = "Employees").next_sibling,
    "manager" :soup.find('span',string = "Company manager").next_sibling
}
print(company)

я получаю эту ошибку ниже "Traceback (последний вызов был последним): файл" C:\ Python27 \ yelu.py ", строка 14, в" company_name ": soup.select_one ('# company_name'). Text, AttributeError: у объекта 'NoneType' нет атрибута 'text'

"

1 Ответ

0 голосов
/ 31 декабря 2018

Ваша конструкция URL неверна.Избавьтесь от двойного // in

in/" + href

т.е.:

in" + href

Если вы проверите текущий ответ, вы получите страницу, не найденную.

См. Вывод из:

from bs4 import BeautifulSoup 
import requests

headers = {'User-Agent': 'Googleboat'}
r = requests.get('https://www.yelu.in/category/advertising') 
soup = BeautifulSoup(r.text,'lxml')

for links in soup.find_all('a',class_='m_company_link'): 
    href = links['href']
    try:
        r = requests.get("https://www.yelu.in/" + href, headers = headers)
        soup = BeautifulSoup(r.text,'lxml')
        company = {
        "company_name" : soup.select_one('#company_name').text,
        "address" : soup.select_one('div.text.location').text,
        "phone" : soup.select_one('div.text.phone').text,
        "mobile_phone" : soup.find('div',string = "Mobile phone").find_next_sibling('div').text,
        "fax": soup.find('div',string = "Fax").find_next_sibling('div').text,
        "website" : soup.find('div',string = "Website").find_next_sibling('div').text,
        "year" :soup.find('span',string = "Establishment year").next_sibling,
        "employees" :soup.find('span',string = "Employees").next_sibling,
        "manager" :soup.find('span',string = "Company manager").next_sibling
         }
        print(company)
    except AttributeError as e:
        print("https://www.yelu.in/" + href, r.status_code)

Пример вывода:

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...