【问题标题】:Having trouble extracting parsed url in files for more scraping and parsing无法在文件中提取解析的 url 以进行更多的抓取和解析
【发布时间】:2021-01-26 20:09:37
【问题描述】:

问题介绍语言:Python 3.8

操作系统:Windows 10

任何其他相关软件:Jupyter notebook 和 html-requests

问题陈述 我有一个包含抓取网页的 excel 文件文件夹。我的目标是通过从文件中提取 url 并对其进行更多解析以获取更多信息,从而了解有关抓取和解析的更多信息。

test_database=pd.read_csv('questionsonpage27.csv')
test_database.shape
len(test_database)

隔离网址:

test_database.iloc[:,4]

返回一个网址

我尝试过的事情: 第一次尝试:最初我使用了“.iloc[0,4]”,它返回了几乎正确数量的文件和几乎正确的内容(小步骤)。我认识到这仅使用第一行的 url 作为每个文件的内容,但是...

number=len(test_database)
    
for i in range (1,number):
    url = test_database.iloc[0,4]
    r = requests_html.HTMLSession().get(url)
    with open (f"questionparse{i}.csv", 'w') as f:
            data = []
            for tag in r.html.find('.container'):
                data.append(
                    dict(
                        post = tag.find('.post-layout', first=True).text, 
                       # votes    = tag.find('.vote strong',        first=True).text, 
                        #tags     = tag.find('.tags',               first=True).text, 
                        #summary  = tag.find('.excerpt',            first=True).text, 
                        #url      = tag.find('.question-hyperlink', first=True).absolute_links.pop()
                    )
                )
                df=pd.DataFrame.from_dict(data)
                df.to_csv(f"questionparse{i}.csv", index=False)
            #page+=1
            time.sleep(1.3)

使用正确的 .iloc[:,4] 产生了一个错误,所以我四处寻找并想出了我认为更接近但我什至没有得到结果的 SECOND ATTEMPT:

for i in test_database:
    url = f"test_database.iloc[{i},4]"
    r = requests_html.HTMLSession().get(url)
    with open (f"questionparse{i}.csv", 'w') as f:
            data = []
            for tag in r.html.find('.container'):
                data.append(
                    dict(
                        post = tag.find('.post-layout', first=True).text, 
                       # votes    = tag.find('.vote strong',        first=True).text, 
                        #tags     = tag.find('.tags',               first=True).text, 
                        #summary  = tag.find('.excerpt',            first=True).text, 
                        #url      = tag.find('.question-hyperlink', first=True).absolute_links.pop()
                    )
                )
                df=pd.DataFrame.from_dict(data)
                df.to_csv(f"questionparse{i}.csv", index=False)
            #page+=1
            time.sleep(1.3)

预期结果是隔离该列中的 url 并对其进行更多排序。

实际结果:错误信息很广泛,但主要集中在

InvalidURL                                Traceback (most recent call last)
<ipython-input-17-560a6ce41e98> in <module>
      9 for i in test_database:
     10     url = f"test_database.iloc[{i},4]"
---> 11     r = requests_html.HTMLSession().get(url)
     12     with open (f"questionparse{i}.csv", 'w') as f:
     13             data = []

我也试过

url = test_database.iloc[f"{i}",4] 但这甚至没有传递给 r=requests_html.... 我也试过 test_database.iloc[i,4] 但这也没有用。有什么推荐吗?

【问题讨论】:

    标签: python pandas parsing web-scraping python-requests


    【解决方案1】:

    原来 test_database.iloc[i,4] 是正确的格式,我不知道为什么它第一次失败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多