Python方法URL参数

2024-06-08 13:40:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python编写一个提要抓取程序,在控制台上运行该程序时出现语法错误。 我使用的是python 3.8,我使用pip install命令安装了请求和bs4。 错误:

  File "scraping.py", line 4
    def hackernews("https://news.ycombinator.com/rss"):
                                                    ^
SyntaxError: invalid syntax

代码如下:

import requests
from bs4 import BeautifulSoup

def hackernews_rss('https://news.ycombinator.com/rss'):
    try:
        r = requests.get()
        return print('The scraping job succeeded: ', r.status_code)
    except Exception as e:
        print('The scraping job failed. See exception: ')
        print(e)
print('Starting scraping')
hackernews_rss()
print('Finished scraping')

预期产出应为:

starting scrapping 
2000
finished scraping

Tags: thehttpsimport程序comdefjobrequests
3条回答

为此,请将url设置为变量

link = "https://news.ycombinator.com/rss"

然后将其作为函数内的参数调用

def hackernews(link):
    req = requests.get(link)

然后确保在代码底部打印时再次调用链接:

print("started scraping")
hackernews(link)
print("finished scraping")

定义python函数的方法是

def function_name(args):
    function_code
    return function_result
// while calling function
result = function_name("some args")

在代码中,您正在证明定义中的参数。这是无效的语法。 请跟随Tutorial了解更多信息

定义采用标识符,调用为其提供值。标识符提供了将用于引用函数体中带有值的被调用函数的名称

def hackernews_rss(url):
    try:
        r = requests.get(url)
        return print('The scraping job succeeded: ', r.status_code)
    except Exception as e:
        print('The scraping job failed. See exception: ')
        print(e)

print('Starting scraping')
hackernews_rss('https://news.ycombinator.com/rss')
print('Finished scraping')

如果要硬编码要使用的URL,可以使用默认参数值(允许您在调用时调用时指定URL,而无需使用默认参数):

def hackernews_rss(url='https://news.ycombinator.com/rss'):
    try:
        r = requests.get(url)
        return print('The scraping job succeeded: ', r.status_code)
    except Exception as e:
        print('The scraping job failed. See exception: ')
        print(e)

print('Starting scraping')
# The following is equivalent to hackernews_rss('https://news.ycombinator.com/rss')
hackernews_rss()
print('Finished scraping')

或者更直接(并且没有覆盖它的选项)

def hackernews_rss():
    try:
        r = requests.get('https://news.ycombinator.com/rss')
        return print('The scraping job succeeded: ', r.status_code)
    except Exception as e:
        print('The scraping job failed. See exception: ')
        print(e)

print('Starting scraping')
# The following is equivalent to hackernews_rss('https://news.ycombinator.com/rss')
hackernews_rss()
print('Finished scraping')

在这三种情况下,都需要向requests.get()传递一个URL,没有参数就不能调用它

相关问题 更多 >