ke59ka77
@ke59ka77 (kei katsumata)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

python でBeautifulSoupでスクレイピングするときにlxmlがうまく使えないエラーが出る

解決したいこと

python でBeautifulSoupでスクレイピングするときにlxmlがうまく使えないエラーが出るのを解決したい。

各バージョン
Python 3.9.0
beautifulsoup4 4.9.3
lxml 4.6.3

発生している問題・エラー

FeatureNotFound                           Traceback (most recent call last)
<ipython-input-12-60fbc62a1681> in <module>
     11     url='https://ai-scraping.herokuapp.com/index?page='+str(i)
     12     html=requests.get(url)
---> 13     soup = BeautifulSoup(html, "lxml")
     14 
     15     trs=soup.table.find_all("tr")

~/.pyenv/versions/3.9.0/lib/python3.9/site-packages/bs4/__init__.py in __init__(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, element_classes, **kwargs)
    241             builder_class = builder_registry.lookup(*features)
    242             if builder_class is None:
--> 243                 raise FeatureNotFound(
    244                     "Couldn't find a tree builder with the features you "
    245                     "requested: %s. Do you need to install a parser library?"

FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

該当するソースコード

import csv
import requests
import time
from bs4 import BeautifulSoup

i=1
csv_file=open("output.csv", "wt", newline="", encoding="utf-8")
csv_write=csv.writer(csv_file)
while i <= 25:
    url='https://ai-scraping.herokuapp.com/index?page='+str(i)
    html=requests.get(url)
    soup = BeautifulSoup(html, "lxml")

    trs=soup.table.find_all("tr")
    for tr in trs:
        csv_data=[]
        for cell in tr.find_all(["td", "th"]):
            csv_data.append(cell.get_text())
        csv_write.writerow(csv_data)
        time.sleep(3)
        i+=1
    csv_file.close()

自分で試したこと

import lxmlを文頭に追加
lxmlの別バージョンをダウンロード(うまくいかず)

0

1Answer

Comments

  1. @ke59ka77

    Questioner

    リンク先の原因2の対処法で無事該当エラーは突破し、そのほかのエラーも問題なく突破できました〜
    本当にありがとうございました〜

Your answer might help someone💌