【问题标题】:Why can't I crawl the page?为什么我无法抓取页面?
【发布时间】:2018-10-17 19:08:17
【问题描述】:

我正在尝试抓取网站上的表格,然后将其转换为 CSV 表单。尽管有我的代码,但什么都没有出现。你能告诉我出了什么问题吗?

网址:http://www.multiclick.co.kr/sub/gamepatch/gamerank.html

不用担心语言。请在日历上比今天早一两天的任何时间设置日期,然后单击放大镜。然后你就可以看到一张桌子了。

# Load the required modules
import urllib
from bs4 import BeautifulSoup
import pandas as pd

# Open up the page
url = "http://www.multiclick.co.kr/sub/gamepatch/gamerank.html"
web_page = urllib.request.Request(
        url,
        data = None, 
        headers={'User-Agent': ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) "
                                "AppleWebKit/537.36 (KHTML, like Gecko) " 
                                "Chrome/35.0.1916.47 Safari/537.36")})
type(web_page)
web_page = urllib.request.urlopen(web_page)

# Parse the page
soup = BeautifulSoup(web_page, "html.parser")
print(soup)

# Get the table
    # Get the columns
    # Get the rows
    # Stack them altogether

# Save it as a csv form

【问题讨论】:

  • 你不能像那样抓取动态加载内容的页面。只是 fech http://ws.api.thelog.co.kr/service/info/rank/RRRR-MM-DD(它只是 JSON)。
  • 或者,如果你必须抓取,你可以使用 Selenium 来自动化浏览器并通过 ajax/动态页面工作

标签: python web-crawler


【解决方案1】:

是@mx0说的,不是取mian页面,而是取ajax调用,例如:

import csv
import requests

link = "http://ws.api.thelog.co.kr/service/info/rank/2018-10-18"

req = requests.get(link)
content = req.json()
with open('ranks.csv', 'w', newline='') as csvfile:
    csv_writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    # write column titles
    csv_writer.writerow(['gameRank', 'gameName', 'gameTypeName', 'gameShares', 'publisher', 'gameRankUpDown'])
    # write values
    for row in content["list"]:
        csv_writer.writerow(list(row.values()))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    相关资源
    最近更新 更多