Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用Python从USDA_AMS网页提取所有HREF_Python 3.x_Url_Web Scraping - Fatal编程技术网

Python 3.x 如何使用Python从USDA_AMS网页提取所有HREF

Python 3.x 如何使用Python从USDA_AMS网页提取所有HREF,python-3.x,url,web-scraping,Python 3.x,Url,Web Scraping,我试图从美国农业部AMS网站上提取一些URL以获得一些报告。下面是一个示例链接:。我需要URL以“.txt”结尾 下面是我正在使用的Python(Python3)代码: Slug_ID = '3040' url = 'https://mymarketnews.ams.usda.gov/viewReport/' + Slug_ID ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.

我试图从美国农业部AMS网站上提取一些URL以获得一些报告。下面是一个示例链接:。我需要URL以“.txt”结尾

下面是我正在使用的Python(Python3)代码:

Slug_ID = '3040'
url = 'https://mymarketnews.ams.usda.gov/viewReport/' + Slug_ID
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
html = urllib.request.urlopen(url, context=ctx).read()
txt_urls = re.findall(b'href="(http[s]?://mymarketnews.ams.usda.gov/.*?)"', html)
几周前,它对其他几个报告(不同的Slug ID)有效,但现在不行了。不知道为什么

我还尝试使用beautifulSoap从网站上查找所有的HREF

web_r = requests.get(url)
tags = web_soup.findAll('a')
for tag in tags:
    print(tag.get('href', None))
它以43个链接检索回来。没有一个是我正在寻找的url。我知道当我进行“检查”时,我需要一些URL。例如:

...
<ul class = "section-data-month">
    <li> 
        MS_GR215, [04/07/2020], 04/07/2020 16:38:33, [
        <a href="https://mymarketnews.ams.usda.gov/filerepo/sites/default/files/3040/2020-04-07/169146/ams_3040_00007_01.txt" target= "_blank">
        <span class = "text-uppercase">TXT</span>
        </a>]
     </li>
    <li>...</li>
</ul>
...
。。。
  • MS_GR215,[04/07/2020],04/07/2020 16:38:33[ ]
...
我需要的URL看起来像:“”

有人能帮我吗?我真的是一个Python新手。非常感谢