首页 > 解决方案 > 使用 BeautifulSoup 抓取时,我得到一个 0kb 的 TXT 文件

问题描述

我正在尝试从此代码中抓取这些信息,但输出为空:

<td class="views-field views-field-subscription-client-name">
                    <div>
<strong>ANNA Elliassaint René et Marjorie</strong>
<div class="show-details row-details-3390489 collapse in" aria-expanded="true" style="">
    <p>Tél : 0671328654</p>
    <p>kartelizi9@gmail.com</p>
</div>

这是我抓取这些信息的代码:

import urllib2
from bs4 import BeautifulSoup
for x in range(1,4):
        numb = str(x)
        url = "https://fr.eni.com/appel-entrant-particuliers/?page="+numb
        page = urllib2.urlopen(url).read()
        soup = BeautifulSoup(page, 'html.parser')
        html =soup.contents
        html = soup.prettify("utf-8")
        records = []
        #try:
        for scrape_email in soup.find_all('td', attrs={'class': 'views-field views-field-subscription-client-name'}):
                scrape = scrape_email.text
                records.append(scrape)
        for scrape in records:
                        f = open("myfile.txt", "a+")
                        f.write(scrape)
                        f.write("\n")
                        f.close()
print("OK" +(numb))

我想刮掉所有 4 页上的所有信息,请提供任何解决方案

标签: python-2.7for-loopweb-scrapingbeautifulsoup

解决方案


推荐阅读