刮取网址列表并确认其是否包含2个设置短语

时间:2017-06-23 12:57:12

标签: python web-scraping beautifulsoup

这是我的第二个问题,后面是yesterdays

我试图通过requests.get和BeautifulSoup来做这件事而没有太多运气。

我的目标:

从我之前的post开始,我有一个字符串列表,我想通过这些字符串将每个字符串附加到一个URL,然后废弃该URL以确定它是否包含两个短语。

短语

我正在寻找的短语是:

  • TOYOTA
  • SILVER

(其他任何内容都可以被拒绝,并使用下一个网址开启。)

网址

网址为" https://vehicleenquiry.service.gov.uk/ConfirmVehicle?Vrm= " + STRING

实施例

  1. https://vehicleenquiry.service.gov.uk/ConfirmVehicle?Vrm=CV05LWT
  2. https://vehicleenquiry.service.gov.uk/ConfirmVehicle?Vrm=CV05LWX
  3. https://vehicleenquiry.service.gov.uk/ConfirmVehicle?Vrm=CV05LWY
  4. HTML

    <ul class="list-summary margin-bottom-2">
      <li class="list-summary-item">
        <span>Registration number</span>
        <span class="reg-mark">CV05 LWT</span>
      </li>
      <li class="list-summary-item">
        <span>Make</span>
        <span><strong>TOYOTA</strong></span>
      </li>
      <li class="list-summary-item">
        <span>Colour</span>
        <span><strong>SILVER</strong></span>
      </li>
    </ul>
    

    最后步骤

    任何包含这两个短语的网址都需要将原始字符串(例如CV05LWT )复制到新列表中,以便日后进一步处理。

2 个答案:

答案 0 :(得分:0)

使用BeautifulSoup,您必须检查要捕获的页面元素。对于你,当你达到“模式C”,一个成功的结果,你需要检查品牌和颜色是否正确。在没有看到你的代码的情况下,我无法正确地添加它,但这里有一个它应该看起来的例子......

Make = BeautifulSoup(subreq.text).find('td', id = 'Make')
Colour = BeautifulSoup(subreq.text).find('td', id = 'Colour')

if (Tag Found)
   if (Color == "color you need") && (Make == "Make you need")
       Add to list
else
   Move to the next Tag

答案 1 :(得分:0)

如果我们召唤您的yersterday's post,您可以使用以下代码执行网页报废问题:

import BeautifulSoup as bs
import requests
import lxml.html
import re
import itertools

List1 = ['CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY']
List2 = ['51', '02', '52', '03', '53', '04', '54', '05', '55', '06', '56', '07', '57', '08', '58', '09', '59', '10', '60', '11', '61', '12', '62', '13', '63', '14', '64', '15', '65', '16', '66', '17']
List3 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List4 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
s = [List1, List2, List3, List4, List5]
marks = list(itertools.product(*s))

for mark in marks:
    mark = "".join(mark)
    url = "https://vehicleenquiry.service.gov.uk/ConfirmVehicle?Vrm=" + mark
    print "Checking " + mark + "..."

    result = requests.get(url)

    marks_match = list()

    soup = bs.BeautifulSoup(result.content)
    html = soup.findAll("li", "list-summary-item")

    if len(html) == 3:
        make = lxml.html.document_fromstring(str(html[1])).text_content()
        make = re.findall(r'\n.*\n(.*)',make)[0]

        colour = lxml.html.document_fromstring(str(html[2])).text_content()
        colour = re.findall(r'\n.*\n(.*)',colour)[0]

        if make == "TOYOTA" and colour == "SILVER":
            marks_match.append(mark)
            print mark + " is TOYOTA SILVER"

print "TOYOTA SILVER cars: " + str(marks_match)

列表marks_match将包含您要查找的注册号。

当然需要一段时间才能完成。