python - Soup-ify 获取请求

标签 python html pandas web-scraping beautifulsoup

我正在尝试 soup-ify 获取请求

from bs4 import BeautifulSoup
import requests 
import pandas as pd

html_page = requests.get('"https://www.dataquest.io"')

soup = BeautifulSoup(html_page, "lxml")
soup.find_all('<\a>')

但是,这只会返回一个空列表

最佳答案

这将拉取表行并将每一行分配给一个字典,该字典附加到一个列表中。您可能需要稍微调整选择器。

from bs4 import BeautifulSoup
import requests
from pprint import pprint

output_data = [] # This is a LoD containing all of the table data

for i in range(1, 453): # For loop used to paginate
    data_page = requests.get(f'https://www.dataquest.io?')
    print(data_page)

    soup = BeautifulSoup(data_page.text, "lxml")

    # Find all of the table rows
    elements = soup.select('div.head_table_t')
    try:
        secondary_elements = soup.select('div.list_table_subs')
        elements = elements + secondary_elements
    except:
        pass
    print(len(elements))
    # Iterate through the rows and select individual column and assign it to the dictionary with the correct header
    for element in elements:
        data = {}
        data['Name'] = element.select_one('div.col_1 a').text.strip()
        data['Page URL'] = element.select_one('div.col_1 a')['href']
        output_data.append(data) # Append dictionary (contact info) to the list
        pprint(data) # Pretty Print the dictionary out (to see what you're receiving, this can be removed)

关于python - Soup-ify 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59067700/

相关文章:

python - 通过 Pandas 中的坐标检查点是否在 3D Box 中

python - 在 Python 进程之间共享内存中的大型数据结构?

python - 在Python中转换嵌套的字典列表

html - 无法使用 overflow-x : hidden on image

javascript - 用户捕获 div 作为图像并保存到计算机

javascript - HTTP 错误 405.0 - 不允许的方法 - 表单标记

python - Pandas/Keras : use data from DataFrame to train Keras model, 输入形状错误

python - 使用 Pandas ,我如何比较两个数据帧的两列之间的值并将它们推送到新的数据帧?

python 更改数字格式 ndarray 许多位

python - 使用 TI Chronos 在 Python 中打开端口时出错