【问题标题】:Trying to output JS data from webpage into .html output file试图将网页中的 JS 数据输出到 .html 输出文件中
【发布时间】:2019-10-24 10:41:24
【问题描述】:

我已成功将 JS 中列出的网站抓取到本地 .html 文件中,但输出不足。

问题是:

  • 它只产生最后一个查询(audioSource),而不产生其他请求
  • 它只找到第 1 集,然后停在那里。如何让它重复直到找到结尾?

非常感谢

import requests
import json
from bs4 import BeautifulSoup

JSONDATA = requests.request("GET", "https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=1000000&page=1")
JSONDATA = JSONDATA.json()

for line in JSONDATA['posts']:
    soup = BeautifulSoup(line['episodeNumber'],'lxml')
    soup = BeautifulSoup(line['title'],'lxml')
    soup = BeautifulSoup(line['image']['large'],'lxml')
    soup = BeautifulSoup(line['excerpt']['long'],'lxml')
    soup = BeautifulSoup(line['audioSource'],'lxml')
with open("output1.html", "w") as file:
    file.write(str(soup))

【问题讨论】:

    标签: python html json python-3.x beautifulsoup


    【解决方案1】:

    这里的问题是:

    1. 在写入时使用w,它将整个文件替换为更新的文本。
    2. 对所有值使用相同的变量名soup
    3. 这里不需要bs4模块来解析json数据。

    你可以做的是:

    安装pandas 模块并创建一个数据框。 使用 pip 安装它:pip install pandascondaconda install pandas

    然后您可以使用dataframe 并随心所欲地使用它。

    import requests
    import json
    import pandas as pd
    import os
    
    JSONDATA = requests.request("GET", "https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=1000000&page=1")
    JSONDATA = JSONDATA.json()
    
    df = pd.DataFrame(JSONDATA)
    
    filename = 'Output.txt'
    os.mknod(filename) #create the filename above.
    
    with open(filename, 'a') as fopen:
        for i in range(len(df)):
            fopen.writelines(df.posts[i]['episodeNumber']+'\n')
            fopen.writelines(df.posts[i]['title']+'\n')
            fopen.writelines(df.posts[i]['image']['large']+'\n')
            fopen.writelines(df.posts[i]['excerpt']['long']+'\n')
            fopen.writelines(df.posts[i]['audioSource']+'\n')
            fopen.writelines("\n")
    fopen.close()
    

    这是您想要的完整代码。
    此外,您可以使用print(df.head()) 查看数据框如何将值存储为字典并执行更多操作。

    输出:

    你可以看到全文here

    【讨论】:

    • 谢谢。运行它会给我这个输出错误:文件“./test4.py”,第 19 行,在 fopen.writelines(df.posts[i]['excerpt']['long']+'\n') TypeError: writelines() argument must be a sequence of strings 它生成文件,但只生成前 3 个输出并且没有摘录或 audioSource 链接?此外,它只产生第一个结果 - 即第 116 集,我如何让它重复到最后,即第 1 集?
    • 这是不言自明的。您需要传递字符串值。所以你可以做fopen.writelines(str(df.posts[i]['excerpt']['long']+'\n'))
    【解决方案2】:

    使用pandas库,将数据保存到当前项目目录下的CSV文件中

    import requests
    import pandas as pd
    
    resp = requests.get("https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=1000000&page=1").json()
    df = pd.DataFrame(resp['posts'], columns=['episodeNumber', 'title', 'image','excerpt','audioSource'])
    #it will save data into post csv file and stored in current project directory
    df.to_csv("posts.csv")
    

    【讨论】:

      猜你喜欢
      • 2020-05-12
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多